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 are the c++ access specifiers?
Can malloc be used in c++?
What are inline functions? What is the syntax for defining an inline function?
What is c++ and its uses?
Can I learn c++ without c?
Is string data type in c++?
Will rust take over c++?
Why do we learn c++?
Can we use struct in c++?
How many types of scopes are there in c++?
Can we overload operator in c++?
Why do we need constructors in c++?
Does std endl flush?
How do you find out if a linked-list has an end? (I.e. The list is not a cycle)
Why use of template is better than a base class?