For any of you who are competent with C++, I have a few questions that I need answered, and hopefully you can provide me with them.
- What is a destructor, where and why would I use it?
- What are #ifdef and #endif used for?
- What is 'this' used for, and when and why is it used?
These are some of the questions that I can't find on the Internet, and because the C++ group I created seems to be pretty quiet as well, the only place I could really ask these questions was here.
|
A destructor is called when you delete an object. You define the destructor typically to deal with anything that would be required to clean up memory when the object is destroyed. You have to manage memory manually in C++. You should allocate memory in the constructor and deallocate in the destructor, and delete the object when it is no longer in use.
ifdef and endif are preprocessor commands.
If I were to write something like
#ifdef WINDOWS_VERSION
//Insert platform-specific Windows code here.
#endif
#ifdef LINUX_VERSION
//Insert platform-specific Linux code here.
#endif
Then I can change the codebase at compile time by defining the flag WINDOWS_VERSION or LINUX_VERSION. In this example, I'd use this so I wouldn't have to have two different project files for a Linux and Windows version, because most of the code is platform-independent anyway.
"this" refers to the instance of the class you're working with. If I define a member function for a "car" class that has "wheels" as a variable, I can reference it by using either "wheels" or "this->wheels". If there is another variable in scope with the name "wheels", then "this->wheels" will always refer to the variable associated with the object by which the member function belongs to.