Explain the need for "Virtual Destructor"?
Answers were Sorted based on User's Feedback
Answer / lylez00
If A is a base class, and from that, B is derived, and a
dynamically allocated object of type B is deleted via a
pointer of type A, then B's destructor will not be invoked
unless A's destructor is virtual.
A *a = new B();
delete a; // won't invoke B's destructor unless A's
destructor is virtual
| Is This Answer Correct ? | 12 Yes | 3 No |
Answer / 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 |
Do the names of parameters have to agree in the prototype, definition, and call to the function?
Difference between Operator overloading and Functional overloading?
What are the strengths of C++?
Can constructor be private in c++?
Write a short code using c++ to print out all odd number from 1 to 100 using a for loop
How much do c++ programmers make?
What is an operator in c++?
How we can differentiate between a pre and post increment operators during overloading?
Can a constructor throw a exception? How to handle the error when the constructor fails?
What is the protected keyword used for?
What is the difference between while and do while loop? Explain with examples.
Why is it difficult to store linked list in an array?