对于以下代码,为什么会打印两次“A is closed: 3”?
#include <stdio.h>
class A
{
public:
int a;
A(int n)
{
a = n;
printf("This is A: %d.\n", a);
}
A()
{
a = 0;
printf("This is A: %d.\n", a);
}
~A()
{
printf("A is closed: %d.\n", a);
}
};
class B
{
public:
A a;
B()
{
a = A(3);
}
};
void f()
{
B b;
}
int main()
{
f();
return 0;
}
输出:
This is A: 0.
This is A: 3.
A is closed: 3.
A is closed: 3.