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 |
Define vptr.
How does com provide language transparency?
What is microsoft c++ redistributable 2013?
is throwing exception from a constructor not a good practice ?
In a function declaration what does extern means?
In C++ cout is: a) object b) class c) something else
11 Answers Infosys, Lehman Brothers,
What are the basics of local (auto) objects?
What is the purpose of the noexcept keyword?
Explain the difference between 'operator new' and the 'new' operator?
Can member functions be private?
What is scope of a variable? (LOLZ)
How can a called function determine the number of arguments that have been passed to it?