Calling a Method in Constructor

54,930

Solution 1

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?

Solution 2

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.

Solution 3

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)

Solution 4

The implication from the lifetime not having started yet is mainly that, should the constructor throw an exception, the destructor will not be run.

Share:
54,930

Related videos on Youtube

Arun
Author by

Arun

Updated on July 09, 2022

Comments

  • Arun
    Arun almost 2 years

    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, and the object not only does not now exist, but never existed.
    
  • razlebe
    razlebe over 13 years
    You're assuming that the OP didn't read the article carefully. Without seeing the article, you don't know whether it explained the situation well, if at all.
  • Nawaz
    Nawaz over 13 years
    @sgreeve: If he asked this question, does that not imply that he didn't read the article "carefully" since he doesn't seem to know what Herb Sutter might actually have said? And no, that was not harse; it's just yet another way to tell him that he should read the article once again.
  • razlebe
    razlebe over 13 years
    I'm afraid not, no. I'm sure you didn't mean to sound harsh or condescending - but to my mind, it was a bit.