Alex Rivera | Logout

Calling a Method in Constructor

Asked 2011-02-02T09:32:39.037
31

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 a crude way control passes beyond its final brace.

Now consider the following code

class A
{
  public:
  A() 
  { 
      f();
  }

  void f() 
  { 
      cout << "hello, world"; 
  }

}; 

int main()
{
   A a;
}

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.

Still there is indeed a valid "this" inside the constructor and f() does get called.

I don't think Herb is saying something incorrect... but guess i am interpreting it incorrectly....can some explain to me what exactly that is?

Here is the link to the article : http://www.gotw.ca/gotw/066.htm It talks about exceptions from constructors. Specifically here is the extract from it on which my question is based:

-When does an object's lifetime begin? When its constructor completes successfully and returns normally. That is, control reaches the end of the constructor body or an earlier return statement.

-When does an object's lifetime end? When its destructor begins. That is, control reaches the beginning of the destructor body. Important point here is that the state of the object before its lifetime begins is exactly the same as after its lifetime ends -- there is no object, period. This observation brings us to the key question:

We might summarize the C++ constructor model as follows:

Either:

(a) The constructor returns normally by reaching its end or a return statement, and the object exists.

Or:

(b) The constructor exits by emitting an exception, 
Edit
Report

1 Answer

4

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:

  • external: ie the user of an object
  • internal: ie, you when writing constructors and destructors (notably)

From the external point of view, the lifetime of an object:

  • begins once the constructor successfully completed
  • ends when the destructor begins to run

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).

  • in the body of the constructor, you can use the attributes and bases of the class (they are initialized), and call functions normally (virtual calls should be avoided).
  • if it's a base class, the derived object is not initialized yet (thus the restriction on virtual calls)
answered 2011-02-02T11:00:28.420

Your Answer