Search This Blog

Tuesday, July 13, 2010

Constructor Destructor and Virtual Keyword (C++)

One of the favorite interview question is -
Why destructor should be always virtual and constructor should never be virtual?

Let's discuss these 2 questions separately.

Before we start keep this class hierarchy in mind -

                                 Base
                                   ^
                                    |
        -------------------------------------------
       |                            |                             |
Derived1               Derived2                Derived3



1. Why is constructor never virtual?
While creating any object programmer always 'knows' which object he is creating i.e. Which class we want to instantiate.
e.g. Base *pBase = new Derived3();  // Though the pointer is of Base class type the programmer very
                                                         // well knows to write new Derived3()

In addition, also note that though virtual table in not specific to class instance (object), to call virtual function of any class first we need valid object of that class.
i.e. Virtual function can not be called unless we have constructed the object.

2. Why is destructor be always virtual?
There might be function of the type
void do_something_and_then_delete(Base *pBase) {
     // Do something with pBase

     delete pBase;
}

Now it is easy to understand that user might write some code like -
Base* pBase = new Derived1();
do_something_and_then_delete(pBase);
Base* pBase = new Derived2();
do_something_and_then_delete(pBase);
Base* pBase = new Derived3();
do_something_and_then_delete(pBase);

In all 3 cases correct destructor needs to get called.
User always knew name of the class to instantiate, but while deleting user did not have idea about which is the exact destructor he wants to call.
If the destructors of Derived classes are not virtual then the destructor of Base class will get called and it will lead to memory leaks and might be some other problems in the system.

If you have any better answer to improve this post please comment, I will modify the post.

1 comment:

  1. I'd just like to add that .. having a virtual constructor doesn't make any sense!! Though, exactly that's what you are saying, I just wanted to say it specifically :)

    ReplyDelete