Explain the need for "Virtual Destructor"?
Answer Posted / p govind rao
A destructor can be declare virtual. virtual destructor is
mainly useful during inheritance.
class base
{
public:
base(){}
virtual ~base(){}
};
class derv
{ char *p;
public :
derv(){ptr=nes char[2];}
~derv(){delete ptr;}
} ;
main()
{
base *baseptr=new derv();
delete baseptr;
}
If base class, and derived class, and a dynamically
allocated object of type derived is deleted via a pointer
of type base, then derived's destructor will not be invoked
unless base's destructor is virtual.
base *baseptr = new derv();
delete baseptr; // won't invoke B's destructor unless A's
destructor is virtual
| Is This Answer Correct ? | 6 Yes | 0 No |
Post New Answer View All Answers
What is a base class?
What is the use of this pointer in c++?
What is the use of setfill in c++?
Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].
What is a class template?
write a function signature with various number of parameters.
Are strings immutable in c++?
What are the differences between the function prototype and the function defi-nition?
Can notepad ++ run c++?
Can you explicitly call a destructor on a local variable?
What is the difference between #import and #include?
Can I have a reference as a data member of a class? If yes, then how do I initialise it?
What is flush c++?
What do you mean by a template?
What are its advantages and disadvantages of multiple inheritances (virtual inheritance)?