VMware Interview Question: Why would you want to have a ... | Glassdoor

Interview Question

Senior Software Engineer Interview Palo Alto, CA

Why would you want to have a Virtual Destructor in C++

c++ algorithms

Interview Answer

1 Answer


Suppose there is virtual base class and inherited ones (Base class B and Inherited classes I1, I2 etc)
if you have base class pointer that points to inherited classes such that
B *b=&i1;
*b=&i2, etc
and at some point of your program you explicitly use delete operator to delete b
it will delete all instances of inherited class objects that were assigned to b
in other words by writing delete b;
you will call destructor off all inherited classes
by making destructor of base class virtual thsi problem will be solved;
so delete b will call destructor only for currently pointing class
(source: Deitel, how to program in C++)

Vilen on Jul 12, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.