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 operator overloading in c++ example?
What are maps in c++?
Should a constructor be public or private?
Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);
Describe exception handling concept with an example?
What are the differences between the function prototype and the function defi-nition?
What is the meaning of c++?
How do you clear a buffer in c++?
What are multiple inheritances (virtual inheritance)? What are its advantages and disadvantages?
Why do we use the using declaration?
What is null pointer and void pointer and what is their use?
What is the importance of mutable keyword?
What are dynamic type checking?
What is diamond problem in c++?
What are separators in c++?