简体   繁体   English

何时在运算符重载示例C ++中调用构造函数和析构函数

[英]When Do Constructors and Destructors Get Called in Operator Overload Example C++

I am trying to figure out exactly when Constructors and Destructors get called. 我试图弄清楚何时构造函数和析构函数被调用。 Sample code: 样例代码:

 #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;
 }

so when I call this, I get the output: 所以当我调用它时,我得到的输出是:

 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

so i of course understand the first block of the output. 所以我当然理解输出的第一块。 I explicitly call the two constructors. 我明确地调用了两个构造函数。 The second block of code, there is the A constructor that gets called when you create the object r 第二个代码块是创建对象r时调用的A构造函数。

 A r(x + rhs.x);
  1. Now when the A destructor gets called here is it because r goes out of scope? 现在,在这里调用A析构函数时,是因为r超出范围了吗? Which means that the destructor is called from within the A operator overload+ 这意味着从A运算符中调用析构函数重载+

Then the 3rd block of output code. 然后是第三个输出代码块。 The constructor B gets called on this line of code. 在此代码行上调用构造函数B。

 return B(x + rhs.x);
  1. Now my biggest question is why does the B destructor get called twice here? 现在我最大的问题是,为什么B析构函数在这里被调用两次? Where does it get called from- main() or the B operator overload+ ? 它从哪里被调用-main()或B运算符重载+?

In the second block of code, the destructor for A is invoked when the variable r falls out of scope. 在第二段代码中,当变量r超出范围时,将调用A的析构函数。

In the third block of code, you're really constructing two B object. 在第三段代码中,您实际上是在构造两个B对象。 The first is being constructed because you're passing B in by value, which invokes the copy constructor. 第一个正在构造中是因为您要按值传递B,它会调用复制构造函数。 The default copy constructor created by the compiler doesn't include code to print out "Constructor B called", so you're not seeing that the constructor is called in your output. 编译器创建的默认复制构造函数不包含用于打印出“已调用构造函数B”的代码,因此您不会在输出中看到该构造函数被调用。 The second constructor is invoked, as you said, on the return line, after which point both the return value and the rhs variable fall out of scope, and the destructors are called for both objects. 如您所说,在返回行上调用第二个构造函数,此后,返回值和rhs变量都超出范围,并且两个对象都调用了析构函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM