Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is a base class?

1114


What is the use of this pointer in c++?

1007


What is the use of setfill in c++?

1074


Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].

1059


What is a class template?

1082


write a function signature with various number of parameters.

1047


Are strings immutable in c++?

1158


What are the differences between the function prototype and the function defi-nition?

1098


Can notepad ++ run c++?

1085


Can you explicitly call a destructor on a local variable?

1028


What is the difference between #import and #include?

1051


Can I have a reference as a data member of a class? If yes, then how do I initialise it?

1226


What is flush c++?

1008


What do you mean by a template?

1066


What are its advantages and disadvantages of multiple inheritances (virtual inheritance)?

1200