Calling a Method in Constructor

前端 未结 5 1863
情书的邮戳
情书的邮戳 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:04

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

    0 讨论(0)
  • 2020-12-28 16:07

    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)
    0 讨论(0)
  • 2020-12-28 16:14

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 16:24

    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?

    0 讨论(0)
提交回复
热议问题