I don't know if its the same in C++ as it is in C#, but I hit this problem today: If your class is inheriting from a base class, your default constructor will invoke the base class default constructor first. If your base class is depending on a specific variable to be allocated by the inheriting class object, you're going to get a null reference error in the base class.
-> create new ClassObject
-> Set any variables to preset constant values
-> ClassObject.CTOR(Settings)
-> Set any base class variables to preset constant values
-> base.CTOR() code run
<- ClassObject.CTOR code run
<- ClassObject reference returned to caller
Where is the problem?
If any of the base.CTOR() fields need to be initialized with the settings values passed into the ClassObject, you are out of luck because the class constructor code is evaluated AFTER the base class code is run. You can not change the calling order of constructors.
However, if you include an "Initialize(Settings)" method, you can specify the call order:
1. -> create new ClassObject
-> Set any variables to preset constant values
-> ClassObject.CTOR()
-> Set any base variables to preset constant values
-> empty base CTOR run
<- empty class object CTOR code run
<- ClassObject reference returned to caller
2. -> ClassObject.Initialize(Settings)
-> Set any object memory/variables
-> base.initialize(Settings)
->Set any base level memory/variables
3. -> ClassObject.LoadContent()
-> Run any code necessary to pull external assets from disk into main memory
I decided that I could consolidate all of my class initialization code into a constructor. As I found out, this was a bad design decision.
As Frob suggests above, you want to be able to create "empty" objects.
Here are the principles I try to follow:
-I should be able to create a new object which sets the object properties to initial values. The object is empty and ready to be initialized.
-When an object is initialized with settings, memory is allocated and variables are set.
-I should be able to call a "LoadContent()" method which pulls any external assets from disk into main memory. I should be able to call this as many times as I want, but content is pulled from disk only once.
-I should be able to call an "UnloadContent()" method which releases the external assets from main memory. The object state is returned to the same state it was in before LoadContent() was called.
-To prove this, I should be able to call LoadContent() -> UnloadContent() -> LoadContent() as many times as I want without breaking the objects usability.
-Optional: You can use lazy initialization if you keep a boolean flag to keep track of whether or not object content is loaded. The game tries to use the object, realizes the external assets aren't loaded, goes to load the assets, and then resumes usage of the object (I think the RIFT MMORPG uses this technique).