Calling a Method in Constructor

前端 未结 5 1982
情书的邮戳
情书的邮戳 2020-12-28 15:49

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

5条回答
  •  清酒与你
    2020-12-28 16:16

    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.

提交回复
热议问题