简体   繁体   中英

calling constructor of the class in the destructor of the same class

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.\\

Edit : I thank everyone for their answers, I think I didn't cut my point clearly, I knew the result , it will end up recursively and the program can crash, but the question actually is on Destructor allowing to create an object.


using namespace std; class test{ public: test(){ cout<<"CTOR"<<endl; }

~test() {cout<<"DTOR"<<endl; test(); }};

When the following runs

test();

you construct a temporary (new) object that is immediately destroyed when control "passes by the semicolon", the destructor for that temporary object is invoked, which constructs another temporary object, etc., so you get a death spiral of endless recursive calls which leads to a stack overflow and crashes your program.

Prohibiting the destructor from creating temporary objects would be ridiculous - it would severely limit you in what code you could right. Also it makes no sense - the destructor is destroying the current object, and those temporary object are completely irrelevant to it, so enforcing such constrains on them is meaningless.

据我了解,您只是在析构函数中实例化了新的test对象,并使其保持完整。

Static analysis tools are the things which should complain. For me your case is not very different from the following:

void foo();
void bar()
{
   foo();
}
void foo()
{
   bar();
}

I don't know are there any compilers which will complain about above code, but this example is much simpler than yours and there can be many others.

EDIT: In your case the problem is much simpler. It's an ordinary infinite recursion, because the idea of your destructor is somewhat like that:

   ~test()
   {
      cout<<"DTOR"<<endl;
      test tmp();
      tmp.~test(); // infinite recursion.
   }

This code, which gives the test instances a large size, actually produces a stack overflow very quickly, because of the infinite recursion:

#include <iostream>
using namespace std;
class test{
   public:
    int a[10000];
    test(){
     }

~test() {
 test();
 }};

int main() {
    test t;
}

C++ does not require that a warning be issued for infinite recursion, and in general it is very difficult to detect.

I see no reason why it should be illegal, but I admit I'm struggling to come up with a decent example of why I'd do this. To make it "work" you'd need to conditionally call the c'tor rather than unconditionally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM