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
Define anonymous class.
What is the use of endl?
Difference between overloading vs. Overriding
What does override mean in c++?
What is pointer to member?
What is the difference between map and hashmap in c++?
Write a program that takes a 5 digit number and calculates 2 power that number and prints it.
What is microsoft c++ redistributable?
What are the three forms of cin.get() and what are their differences?
Write a program to find the Fibonacci series recursively.
What are the various storage classes in C++?
What is an operator function? Describe the function of an operator function?
What is c++ programming language?
What are enumerations?
Can member data be public?