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 a forward referencing and when should it be used?
What is meant by forward referencing and when should it be used?
Describe public access specifiers?
Can a Structure contain a Pointer to itself?
What is == in programming?
What is the average salary of a c++ programmer?
Why do we use iterators?
What is the main function c++?
What is const pointer and const reference?
What is c++ prototype?
What is a namespace in c++?
Why Pointers are not used in C++?
What is c++ 11 and c++ 14?
How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?
What are the various compound assignment operators in c++?