通过引用-为什么调用此析构函数? [英] Passing by reference - why is this destructor being called?

查看:146
本文介绍了通过引用-为什么调用此析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有关析构函数调用主题的许多问题中,我找不到与我的情况完全相同的东西。

I could not find (of the many questions on destructor calling topics) any that were exactly the same as my situation.

为什么在调用析构函数时调用析构函数传递的参数是参考?我在我认为输出从中执行的代码行下加了注释(大部分在主要代码中)。

Why is the destructor being called when the parameter passed is a reference? I put comments (mostly in main) under the lines of code where I thought the output was executing from.

struct X { // simple test class
   int val;

   void out(const std::string& s, int nv)
   {
      std::cerr << this << "–>" << s << ": " << val << " (" << nv << ")\n";
   }

   // default constructor
   X() { 
      out("X()", 0); 
      val = 0; 
   } 

   X(int v) { 
      val = v; 
      out("X(int)", v); 
   }

   // copy constructor
   X(const X& x) {
      val = x.val; 
      out("X(X&) ", x.val); 
   } 

   // copy assignment
   X& operator=(const X& a)
   {
      out("X::operator=()", a.val); 
      val = a.val; 
      return *this;
   }

   // Destructor
   ~X() { 
      out("~X()", 0); 
   }
};

X glob(2); // a global variable
// Output Line 1: X(int): 2 (2)

X copy(X a) { 
   return a; 
}

main 功能:

    int main()
{
   X loc{ 4 }; // local variable
      // Output Line 2: X(int): 4 (4)
      // ^from X(int v) function
   X loc2{ loc }; // copy construction
      // Output Line 3: X(X&) : 4 (4)
      // ^from X(const X& x) function
   loc = X{ 5 }; // copy assignment 
      // Output Line 4: X(int): 5 (5)
      // ^from X(int v) function
      // Output Line 5: X::operator=(): 4 (5)
      // ^from the '=' operator overload
      // Output Line 6: ~X(): 5 (0) - ???
   loc2 = copy(loc); // call by value and return 
      // Or does Output Line 6 result from here?
   .
   .
   .
}

1)是否由于而调用了此析构函数loc = X {5}; //复制分配或后面的行: loc2 = copy(loc); //通过值调用并返回

1) Is this destructor is being called because of loc = X{ 5 }; // copy assignment or the line after: loc2 = copy(loc); // call by value and return?

2)为什么调用它?根据我的阅读,析构函数仅在以下情况下被调用:

2) Why is it being called? From what I've read, destructors are only called when:

a) names go out of scope
b) program terminates
c) "delete" is used on a pointer to an object

我知道它不是'b'或'c',所以一定是因为某些东西超出了范围。但是我认为复制分配功能不会超出范围。

I know its not 'b' or 'c' so it has to be because something is going out of scope. But I do not think a reference going out of scope from the copy assignment function does this.

推荐答案

您可以看到析构函数复制分配发生后不久就会调用。复制分配完成后,临时文件( x {5} )被销毁。

You can see that the destructor is called soon after the copy assignment has taken place. After the copy assignment is complete, the temporary (x{5}) is destroyed.

来自标准部分关于析构函数:

From the standard's section on destructors:


15.4析构函数

...

12。隐式调用析构函数

(12.1)-对于在程序终止时具有静态存储持续时间的构造对象,

(12.2)-对于具有线程存储的构造对象线程退出时的持续时间,

(12.3)—对于在创建对象的块退出时具有自动存储持续时间的已构造对象,

(12.4)—寿命结束时构造的临时对象。

这篇关于通过引用-为什么调用此析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆