Herb Sutter mentions in one of his http://www.gotw.ca articles that an object is constructed(has valid existence) only if the constructor executes completes.ie to put it in
The implication from the lifetime not having started yet is mainly that, should the constructor throw an exception, the destructor will not be run.
Note: it would have been easier with the exact article, so that we could have some context
Lifetime considerations are actually pretty complicated.
Considering the constructor of an object, there are two different point of views:
From the external point of view, the lifetime of an object:
It means that if you attempt to access an object mid-construction or mid-destruction Bad Things Happen (tm). This is mostly relevant to multi-threaded programs, but may happen if you pass pointers to your object to base classes... which leads to...
...the internal point of view. It's more complicated. One thing you are sure of is that the required memory has been allocated, however parts of the objects may not be fully initialized yet (after all, you are constructing it).
Beware of member variables that are not yet initialized. Beware of virtual functions: the function that you call might not be the one that you expect if the function is virtual and a derived object is created. Other than that, I do not see any problem calling methods from the constructor. Especially the memory for the object has already been allocated.
By the time program flow enters your constructor, the object's memory has been allocated and the this
pointer is indeed valid.
What Herb means, is that the object's state may not have entirely initialized. In particular, if you are constructing a class derived from A
, then that class' constructor will not have been called while you are still inside A's constructor.
This is important if you have virtual member functions, since any virtual function in the derived class will not be run if called from within A's constructor.
Now from what Herb says, can't we say that since A is not completely constructed inside its constructor Calling f() inside the constructor is invalid as the "this" ptr is not ready yet.
That is only when f()
is a virtual
method of class A
or its inheritance hierarchy and you expect the runtime resolution for f()
according to the right object. In simple words, virtual
mechanism doesn't kick in if the method is invoked inside constructor.
If f()
is not a virtual function, there is no harm in calling it from constructor(s) provided you know what exactly f()
does. Programmers usually call class methods like initialize()
from constructor(s).
Can you give me the link to the Herb Sutter's article?