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
Is c++ proprietary?
What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator?
Why c++ is so important?
Give 10 points of differences between C & C++.
Why is that unsafe to deal locate the memory using free( ) if it has been allocated using new?
Write syntax to define friend functions in C++.
Can a program run without main in c++?
What is the best it certification?
How does c++ sort work?
What is the default width for ouputting a long integer using the insertion operator?
What is the equivalent of Pascal's Real a) unsigned int b) float c) char
What is a modifier in c++?
What information can an exception contain?
Can comments be longer than one line?
Describe friend function & its advantages.