简体   繁体   中英

Strange behavior in C++ constructor\destructor

I was playing with C++ class constructor function by using it recursively to print "trauth table". Everything was seem to be normal until I decided "why not using destructor too recursively?". When I implemented printing the "trauth table" with destructor also, I noticed that the destructor is being called every time the control returns from a recurisve call to constructor.

OUTPUT SNIPPET

Calling hello_world class constructor...
000
A destructor call within initialization has been terminated
001
A destructor call within initialization has been terminated
A destructor call within initialization has been terminated
010
...
...

THE CLASS

#define MAX 3
class hello_world
{
    char *const buf;
    int stack_ptr, destructor_calls;
    bool init;
public:

    // The recursive constructor

    hello_world(char *const &str, int i = 0)
        : buf(str), stack_ptr(0), init(false), destructor_calls(0)
    {
        if (i == MAX)
        {
            buf[i] = '\0';
            cout << buf << endl;
        }
        else
        {
            buf[i] = '0';
            hello_world::hello_world(str, i + 1);
            buf[i] = '1';
            hello_world::hello_world(str, i + 1);
        }
    }

    // The recusive destructor

    ~hello_world()
    {
        ++destructor_calls;
        if (!init) { cerr << "A destructor call within initialization has been terminated" << endl; return; }

        int i = stack_ptr;
        if (i == MAX)
        {
            buf[i] = '\0';
            cout << buf << endl;
        }
        else
        {
            buf[i] = '0';
            ++stack_ptr; // since a destructor cannot take parameters
            hello_world::~hello_world();
            --stack_ptr;
            buf[i] = '1';
            ++stack_ptr;
            hello_world::~hello_world();
            --stack_ptr;

            // Printing total number of calls at final call
            if (i == 0) cout << endl << "\"destrucotr_calls\" = " <<
 destructor_calls << endl;
        }
    }

    void unlock()
    {
        init = true;
    }
}; // end of class hello_world

I'm using an int hello_world::stack_ptr to store current number of i parameter, since a destructor cannot have parameters.

My question is: Why the destructor is being called each time the control leaves a recursive call to the constructor? .

Here is my main():

void main()
{
    char buf[MAX + 1];
    cout << "Calling hello_world class constructor..." << endl;
    hello_world h(buf);
    h.unlock();
    cout << "Calling hello_world class destructor..." << endl;
}

I'm using VS2010. You can view the complete code along with its output here.

ADD : I'm attempting to count total number of calling destructor with int hello_world::destructor_calls . I have found that printing trauth table algorithm takes 2 * (2^MAX) - 1 calls and, at the end, destructor_calls exactly equals this value. However, when counting the sentence "A destructor call within initialization has been terminated" in output, we find that it has been output 14 times. So 14 (calls within initialization) + 15 (natural calls of printing trauth table) should equal 29 while destructor_calls equals 15 only (as if the destructor has't been called at initialization !!)

There's no such thing as a recursive constructor. What looks like a recursion is actually creating a new temporary object.

It is possible to call the destructor directly, but if you call a destructor twice on the same object, you get undefined behavior, so you can't call it recursively.

What you will need to do is call another member function within the constructor or destructor. You can then make these other functions be recursive:

class A {
  A()  { construct(); }
  ~A() { destruct(); }
  void construct(int i=0) { ... construct(i+1); ... }
  void destruct(int i=MAX) { ... destruct(i-1); ... }
};

You play with fire...

hello_world::hello_world(str, i + 1);

Create temporary object and then destruct it...

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