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
Difference between Abstraction and encapsulation in C++?
What is c++ prototype?
Write is a binary search tree? Write an algo and tell complexity?
How can we access protected and private members of a class?
Can a Structure contain a Pointer to itself?
What is a storage class? Mention the storage classes in c++.
Does c++ have foreach?
List the issue that the auto_ptr object handles?
What is c++ vb?
How do you master coding?
What is a flag in c++?
Explain the use of this pointer?
Does std endl flush?
Explain the concept of dynamic allocation of memory?
What is scope operator in c++?