Passing “this” to a function from within a constructor?

后端 未结 4 1557
无人共我
无人共我 2021-02-05 02:19

Can I pass \"this\" to a function as a pointer, from within the class constructor, and use it to point at the object\'s members before the constructor returns?

Is it saf

4条回答
  •  旧时难觅i
    2021-02-05 02:46

    Andy, I think you're wrong about the undefined part of the standard.

    When you're in the constructor, "this" is a pointer to an object whose type is the base class of the object you're creating, which means that virtual functions partially implemented in the base class will be called and the pointers in the virtual table won't be followed.

    More info in the C++ Faq Lite...

提交回复
热议问题