Wednesday, September 7, 2011

Stupid Programmer Tricks, C++

Ok,
I've been diving into C++ after not using it for a long time.  There are some pitfalls that have cost allot of time.
Rather than clutter things up with angry exclamation points to show frustration, I'll make the simple statements.


  1. Destructors must be declared virtual in the base class or the child class destructor will not get called.
  2. You can declare a variable in a base class and then declare it in the child class and no compiler error or warning gets generated.  But they are different variables and when you set, NULL, or delete one, the other is untouched, with very ugly side effects.
  3. You must always initialize variables or they will contain garbage.  In particular, you must set pointers to NULL
Now I bet there are compiler flags I could have set or some such, but they are not the default Visual Studio C++ settings. Hope I saved you some time.

TF