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
Do class method definitions?
What are the advantages of using friend classes?
How can a struct in c++ differs from a struct in c?
Explain how a pointer to function can be declared in C++?
What do you mean by “this” pointer?
Do we have to use initialization list in spite of the assignment in constructors?
What is a stack c++?
How is modularity introduced in C++?
Eplain extern keyword?
Why do we use constructor?
How can you differentiate between inheritance and implementation in c++?
Explain about vectors in c ++?
Difference between class and structure.
What do you mean by translation unit?
How do you initialize a string in c++?