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...

What will happen if when say delete this ?

Answer Posted / priya

Using delete this in destructor will lead to recursive loop
which will lead to Stack overflow...so avoid it over
here...however there are few times where your code with
delete this will just work fine..like in the usage of
garbage colletors in C++.Chk the below code...which works
with no issues:
class temp
{
public:
temp(){std::cout<<"Constructor"<<std::endl;}
~temp(){std::cout<<"Destructor"<<std::endl;}
void destroy() {std::cout<<"In class
temp"<<std::endl;delete this;}

};
int main()
{
temp *t = new temp;
t->destroy();
return 0;
}

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Do class method definitions?

1045


What are the advantages of using friend classes?

1152


How can a struct in c++ differs from a struct in c?

1046


Explain how a pointer to function can be declared in C++?

1065


What do you mean by “this” pointer?

1072


Do we have to use initialization list in spite of the assignment in constructors?

1007


What is a stack c++?

1014


How is modularity introduced in C++?

1214


Eplain extern keyword?

1030


Why do we use constructor?

1060


How can you differentiate between inheritance and implementation in c++?

1221


Explain about vectors in c ++?

1033


Difference between class and structure.

1177


What do you mean by translation unit?

1097


How do you initialize a string in c++?

1003