calling constructor of the class in the destructor of the same class
- by dicaprio
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();
}};