What's the best way to...
...control when things are initialized?
Code:
class Base {
some_other_class* handle;
};
Many classes derive from Base, and some want to initialize handle (with a new object), but some want to keep it initialzed to NULL.
Only a small minority of the derived classes want the handle == NULL. If possible I want to set the default behavior as creating a new object for this pointer. This behavior should be overridden for the special derived classes I talked about.
There's a problem... virtual functions won't work properly until all the necessary constructors have been called. So I can't just let my base classes' constructor call a virtual function to initialize handle. At the base constructor's level, only the Base is created. I don't want to have to define the behavior for every leaf, especially since some leaves are not always leaves.
Creating a new object, then deleting it at a lower level constructor would defeat my purpose. (I'm trying to prevent the handle's constructor from taking up valuable memory and time if the derived object is created frequently enough.)
Any ideas?