Calling member functions from a constructor

后端 未结 5 888
我在风中等你
我在风中等你 2020-12-29 08:17

I know this question has a similar title to this: C++: calling member functions within constructor? but I am asking a more general question.

Is it good practice to c

5条回答
  •  轮回少年
    2020-12-29 08:31

    The main problem with this is that the member function has to work with an object that may be only partially initialized. And if it (even accidentally) passes a reference to the object somewhere else, other code has to od the same. This can get pretty confusing and error-prone, especially once you start overriding such a function in a subclass.

    So in general, this practice should be avoided or at least confined to functions that can't be overriden, and they should never pass a reference to the object being constructed to any other code.

提交回复
热议问题