[英]Why does the destructor run twice in C++?
While doing my programming assignments, I seem to be stumbling over basic C++ concepts.在做我的编程作业时,我似乎在基本的 C++ 概念上磕磕绊绊。 I found the bug in my program and it was caused by my destructor running more times than I expected.
我在我的程序中发现了这个错误,它是由我的析构函数运行的次数比我预期的要多造成的。 Here is a code sample demonstrating what I am doing wrong, down to the bare essentials.
这是一个代码示例,展示了我做错了什么,包括最基本的内容。
#include <iostream>
using namespace std;
class A
{
public:
A(int num)
{
number = num;
cout << "A constructed with number " << number << ".\n";
}
~A()
{
cout << "A destructed with number " << number << ".\n";
}
private:
int number;
};
class B
{
public:
B(A pa)
: a(pa)
{
cout << "B constructor run.\n";
}
~B()
{
cout << "B destructor run.\n";
}
private:
A a;
};
int main()
{
A foo(7);
{
B bar(foo);
}
//Pause the program.
system("pause");
}
What I expect to happen is A foo(7);
我期望发生的是
A foo(7);
allocates space on the stack for an A
object named foo
and call the constructor, passing 7
.在堆栈上为名为
foo
的A
对象分配空间并调用构造函数,传递7
。 It assigns 7
to number
and prints output indicating the the constructor ran.它将
7
分配给number
并打印指示构造函数运行的输出。 Now B bar(foo);
现在
B bar(foo);
allocates space on the stack for a B
object named bar
and calls the constructor, passing foo
by value, which is just a container for an int
.在堆栈上为名为
bar
的B
对象分配空间并调用构造函数,按值传递foo
,它只是一个int
的容器。 The constructor assigns the A
parameter passed to it to it's own private data member a
, and prints output to the screen.构造函数将传递给它的
A
参数分配给它自己的私有数据成员a
,并将输出打印到屏幕上。
Now, when bar
goes out of scope at the closing curly brace, I expect bar
's destructor to be called, which prints output to the screen, then calls the destructor for its data members, namely A a
.现在,当
bar
在结束大括号处超出范围时,我希望调用bar
的析构函数,它将输出打印到屏幕,然后为其数据成员调用析构函数,即A a
。 That destructor prints output to the screen, and discards the int number
that it was containing.该析构函数将输出打印到屏幕上,并丢弃它包含的
int number
。
What I expect the output should be:我期望的输出应该是:
A constructed with number 7.
B constructor run.
B destructor run.
A destructed with number 7.
//Destructors should be called in the reverse order of their construction right?
The actual output:实际输出:
A constructed with number 7.
B constructor run.
A destructed with number 7. //This is unexpected.
B destructor run.
A destructed with number 7.
What is causing that extra destruction?是什么导致了这种额外的破坏?
Your B
constructor takes the A
object by value, which means that foo
is copied into the parameter pa
which then gets copied to the member a
.您的
B
构造函数按值获取A
对象,这意味着foo
被复制到参数pa
,然后参数pa
被复制到成员a
。 The compiler has been able to elide one of the copies (argument to the constructor), but the other one cannot be elided and there you have the second A
object that get's destroyed.编译器已经能够删除其中一个副本(构造函数的参数),但另一个不能被删除,并且您有第二个
A
对象被销毁。
Obviously it comes from member data A a;显然是来自会员数据A a; of class B. I guess your doubt is that why not see any construct output from A, because it's constructed with default copy-ctor for class A, It's better to add one copy-ctor for class A, so that you will see the construct procedure.
B 类。我想您的疑问是为什么看不到 A 的任何构造输出,因为它是使用 A 类的默认复制构造函数构造的,最好为 A 类添加一个复制构造函数,以便您看到构造程序。
A(const A& a)
{
number = a.number;
cout << "A copy-constructed with number " << number << ".\n";
}
I hate it when people get cute with "foo" and "bar".我讨厌人们对“foo”和“bar”变得可爱。
But I see two instances of "A" being constructed - one explicitly, the other implicitly by "B".但是我看到“A”的两个实例正在被构造——一个是显式的,另一个是由“B”隐式的。 And two destructors getting invoked.
并且调用了两个析构函数。
What's not to like about this picture ;)?这张照片有什么不喜欢的地方 ;)?
因为在类 B 中,有一个成员A a
所以要析构 B 对象,首先要调用其成员的析构函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.