Hi,
Not sure if
mutexe is still on-line, hopefully he won't mind if I answer.
The definition of when an object is deemed to have been created, is when a constructor completes. If this doesn't happen, it is as if the object never existed.
By using an in constructor parameter initialiser list (not the same thing as
std::initializer_list
), the member variables are initialised
before the object is created, using the arguments provided.
If one doesn't do this, the object is given default initialisation values (
,
""
etc) via a possibly implicit default constructor (including base class ctors), then one has to
re-initialise the values by assignment or calling an alternative constructor
again. This could get expensive if the objects are large.
Also, it is possible to trap exceptions in the constructor parameter initialiser list, by using a function
try
block.
So this is handy if any of the arguments
throw
.
When initialising your class members, make sure you do all of them, and in the same order as in the class definition.
Now we get down to validation: none of the arguments threw an exception, but we want to check valid ranges for the values, or any interdependency between the values, so we can satisfy the invariants. I find it easy to do this in the body of the constructor (or call a private function to do it, from there) and
throw
if there are problems.
Hopefully this has been helpful :+)