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
What is pointer in c++ with example?
What is the use of endl?
How do you instruct your compiler to print the contents of the intermediate file showing the effects of the preprocessor?
Why do we need constructors in c++?
What is function overloading c++?
Which software is best for programming?
Why is c++ awesome?
What is difference between rand () and srand ()?
What are the different types of polymorphism in c++?
what kind of projects are suitable for c and c++
What does h mean in maths?
What is late binding c++?
What is the precedence when there is a global variable and a local variable in the program with the same name?
Define a constructor - what it is and how it might be called (2 methods)?
Explain pass by value and pass by reference.