calling constructor of the class in the destructor of the same class
Posted
by dicaprio
on Stack Overflow
See other posts from Stack Overflow
or by dicaprio
Published on 2010-05-05T11:31:39Z
Indexed on
2010/05/05
11:38 UTC
Read the original article
Hit count: 193
constructor
|destructor
Experts !! I know this question is one of the lousy one , but still I dared to open my mind , hoping I would learn from all.
I was trying some examples as part of my routine and did this horrible thing, I called the constructor of the class from destructor of the same class.
I don't really know if this is ever required in real programming , I cant think of any real time scenarios where we really need to call functions/CTOR in our destructor. Usually , destructor is meant for cleaning up.
If my understanding is correct, why the compiler doesn't complain ? Is this because it is valid for some good reasons ? If so what are they ?
I tried on Sun Forte, g++ and VC++ compiler and none of them complain about it.
using namespace std;
class test{
public:
test(){
cout<<"CTOR"<<endl;
}
~test() {cout<<"DTOR"<<endl;
test();
}};
© Stack Overflow or respective owner