Search code examples
c++polymorphismvptr

Vptr with default constructors vs explicitly declared constructors


The compiler inserts code into the beginning of the constructor that initializes the VPTR. Is this operation influenced by whether the constructor is the default one or it is explicitly declared? Is there any difference between the two?


Solution

  • It will always be inserted, it has to be for polymorphism to work, at least the way compilers typically have it implemented. It will be initialized always, the constructors you declare (or don't declare) are irrelevant.

    Or, hey, maybe it won't (although I do not know any compilers that behave this way), all you need to care about is that polymorphism works, and will always work no matter what constructors you have declared.