【问题标题】:Strange behavior in C++ constructor\destructorC++ 构造函数\析构函数中的奇怪行为
【发布时间】:2012-09-09 22:46:57
【问题描述】:

我正在使用 C++ 类构造函数递归地使用它来打印“真值表”。一切似乎都很正常,直到我决定“为什么不递归地使用析构函数?”。当我还使用析构函数实现打印“真值表”时,我注意到每次控件从对构造函数的递归调用返回时都会调用析构函数。

输出片段

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

课程

#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

我使用int hello_world::stack_ptr 来存储i 参数的当前数量,因为析构函数不能有参数。

我的问题是:为什么每次控件离开对构造函数的递归调用时都会调用析构函数?。

这是我的 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;
}

我正在使用 VS2010。您可以查看完整代码及其输出here.

添加:我正在尝试使用int hello_world::destructor_calls 计算调用析构函数的总数。我发现打印真值表算法需要2 * (2^MAX) - 1 调用,最后,destructor_calls 完全等于这个值。然而,当计算输出中的句子"A destructor call within initialization has been terminated"时,我们发现它已经输出了14次。所以 14(初始化中的调用)+ 15(打印真值表的自然调用)应该等于 29,而 destructor_calls等于 15(好像在初始化时没有调用析构函数!!)

【问题讨论】:

  • 真正的问题是,你为什么要反复调用析构函数?每个对象都可以被破坏一次,而且只能破坏一次。

标签: c++ visual-studio-2010 recursion constructor destructor


【解决方案1】:

没有递归构造函数这样的东西。看起来像递归的东西实际上是在创建一个新的临时对象。

可以直接调用析构函数,但是如果对同一个对象调用两次析构函数,就会得到未定义的行为,所以不能递归调用。

您需要做的是在构造函数或析构函数中调用另一个成员函数。然后,您可以使这些其他函数递归:

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

【讨论】:

  • 好的。但是析构函数呢?也是递归吗?
  • @Desolator:我已经扩展了我的答案来讨论析构函数。
【解决方案2】:

你在玩火……

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

创建临时对象,然后将其销毁...

【讨论】:

  • 这是创建临时文件的正确语法吗?我希望hello_world() 没有前缀hello_world::
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多