Search code examples
c++functionconstructorinstance

C++: Is it possible to call an object's function before constructor completes?


In C++, is it possible to call a function of an instance before the constructor of that instance completes?

e.g. if A's constructor instantiates B and B's constructor calls one of A's functions.


Solution

  • Yes, that's possible. However, you are responsible that the function invoked won't try to access any sub-objects which didn't have their constructor called. Usually this is quite error-prone, which is why it should be avoided.