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 the full form of c++?
What is data hiding c++?
What is a multimap c++?
What is microsoft c++ redistributable 2013?
What is linked list in c++?
How do we balance an AVL Tree in C++?
In c++, what is the difference between method overloading and method overriding?
What is the fastest c++ compiler?
How a pointer differs from a reference?
What is the difference between while and do while loop? Explain with examples.
Is swift faster than go?
I want to write a C++ language program that: 1. Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. The program should work for squares of all side sizes between 1 and 20.
What is friend class in c++ with example?
Explain the register storage classes in c++.
Which software is used for c++ programming?