Skip to content
Related Articles

Related Articles

Improve Article

Is body of a Default Constructor blank in C++?

  • Difficulty Level : Expert
  • Last Updated : 11 Jan, 2019

The answer to this question depends upon 2 scenarios:

  • Scenario 1: When there is a Virtual function in the program: In this scenario, compiler automatically creates virtual table(known as V-Table) and VVPTR(Virtual Void Pointer). V-Table contains the virtual method calls whereas VVPTR contain the address of virtual methods which are present in V-Table, so VVPTR points to methods of V-Table.

    The compiler takes the following steps to initialize VVPTR:-

    1. When compiler gets an indication that virtual functions are used in the code, it will create V-table and VVPTR.
    2. Now to initialise VVPTR compiler generate 7 lines of code which should run whenever compiler comes to know that virtual function is going to use
    3. So the compiler will copy this 7 lines of code inside the constructor, so that just after the execution of the object VVPTR should get initialised so that it can point to V-Table.
    4. Now if no constructor is defined explicitly, then it will copy this 7 lines of code inside default constructor(which compiler will create of it’s own).
    5. Now, it will call the virtual methods.

    Therefore, this makes it clear that the body of Default Constructor is not blank when the code contains virtual functions.

  • Scenario 2: When there is no Virtual function in the program: In this scenario, compiler doesn’t create any V-table or VVPTR. Hence the default constructor reamins empty.

    Conclusion: If program contain virtual functions, then the body of default constructor is not blank and if not, then the body of default constructor is blank

    Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
    My Personal Notes arrow_drop_up
Recommended Articles
Page :