复制构造函数/析构函数的外观-示例

时间:2018-10-26 14:46:35

标签: c++ constructor destructor

我的另一个问题主题(仅供参考。不需要回答此问题。对于其他不知道为什么调用第6输出行的读者更是如此):Passing by reference - why is this destructor being called?

请检查下面的代码(以及表明我理解的注释),以帮助我(及其他人)更好地理解何时调用复制构造函数。

我正在使用Visual Studio 2015作为对编译器的引用。

我认为我无法确定为什么我的原因为何要调用这些构造函数是正确的,这就是为什么我要在StackOverflow上询问您的输入。 / p>

我特别质疑第7-18行(我对第1-6行的推理感到很自信,但是如果我错了,可以随时纠正我)。

要明确:如果我关于为什么在第7-18行出现这些构造函数析构函数的理由不正确,那么为什么调用这些构造函数/析构函数的正确原因是什么?

struct X

struct X { // simple test class
   int val;

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

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

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

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

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

X glob(2); // a global variable

      // Output Line 1: X(int): 2 (2)
      // Reasoning: from X(int v) function

X copy(X a) { 
   return a; 
}

X copy2(X a) { 
   X aa = a; 
   return aa; 
}

main功能:

int main()
{
   X loc{ 4 }; // local variable 

      // Output Line 2: X(int): 4 (4)
      // Reasoning: from X(int v) function

   X loc2{ loc }; // copy construction 

      // Output Line 3: X(X&) : 4 (4)
      // Reasoning: from X(const X& x) function

   loc = X{ 5 }; // copy assignment 

      // Output Line 4: X(int): 5 (5)
      // Reasoning: from X(int v) function

      // Output Line 5: X::operator=(): 4 (5)
      // Reasoning: from the '=' operator overload

      // Output Line 6: ~X(): 5 (0)
      // Reasoning: X { 5 } is not an object. gets terminated at ';'

   loc2 = copy(loc); // call by value and return 

      // Output Line 7: X(X&): 5 (5)
      // Reasoning: from making a copy of X { 5 } for loc

      // Output Line 8: X(X&): 5 (5)
      // Reasoning: from making a copy of "loc" in parameter

      // Output Line 9: ~X(): 5 (0)
      // Reasoning: after copy made in (7) leaves function

      // Output Line 10: X::operator=(): 4 (5)
      // Reasoning: from the '=' operator overload

      // Output Line 11: ~X(): 5 (0)
      // Reasoning: same reason as output (6) above

   loc2 = copy2(loc);

      // Output Line 12: X(X&): 5 (5)
      // Reasoning: from making a copy of X { 5 } for loc

      // Output Line 13: X(X&): 5 (5)
      // Reasoning: from making a copy of "loc" in parameter

      // Output Line 14: X(X&): 5 (5)
      // Reasoning: from creating "aa"

      // Output Line 15: ~X(): 5 (0)
      // Reasoning: "loc" copy from (13) destroyed (see line 6 for reason)

      // Output Line 16: ~X(): 5 (0)
      // Reasoning: X { 5 } copy from (12) destroyed (see line 6 for reason)

      // Output Line 17: X::operator=(): 5 (5)
      // Reasoning: from the '=' operator overload

      // Output Line 18: ~X(): 5 (0)
      // Reasoning: after "aa" copy from (14) leaves function 
.
.
.
}

0 个答案:

没有答案