我试图弄清楚构造函数和析构函数何时被调用。示例代码:
#include <iostream>
using namespace std;
class A{
public:
A();
A(int);
A operator+(const A& rhs) const;
~A();
public:
int x;
};
A A::operator+(const A& rhs) const
{
A r(x + rhs.x);
cout<<"end A operator"<<endl;
return r;
}
A::A(int y): x(y)
{
cout<<"Constructor A called"<<endl;
}
A::~A()
{
cout<<"Destructor A called"<<endl;
}
//////////
class B{
public:
B();
B(int);
B operator+(B rhs) const;
~B();
public:
int x;
};
B B::operator+(B rhs) const
{
cout<<"beginning B operator"<<endl;
return B(x + rhs.x);
}
B::B(int y): x(y)
{
cout<<"Constructor B called"<<endl;
}
B::~B()
{
cout<<"Destructor B called"<<endl;
}
int main()
{
cout<<"line 1 main()"<<endl;
A a(1);
cout<<"line 2 main()"<<endl;
B b(2);
cout<<"\nline 3 main()"<<endl;
a = a + a;
cout<<"\nline 4 main()"<<endl;
b = b + b;
cout<<"\nend of main"<<endl;
}
所以当我调用它时,我得到输出:
line 1 main()
Constructor A called
line 2 main()
Constructor B called
line 3 main()
Constructor A called
end A operator
Destructor A called
line 4 main()
beginning B operator
Constructor B called
Destructor B called
Destructor B called
end of main
Destructor B called
Destructor A called
所以我当然理解输出的第一块。我明确地调用了这两个构造函数。第二个代码块,有一个构造函数,当你创建对象 r 时会调用它
A r(x + rhs.x);
- 现在在这里调用 A 析构函数是因为 r 超出范围吗?这意味着从 A 运算符重载+中调用析构函数
然后是第三个输出代码块。在这行代码上调用构造函数 B。
return B(x + rhs.x);
- 现在我最大的问题是为什么 B 析构函数在这里被调用两次?它是从哪里调用的 - main() 或 B 运算符重载+?