在temporaries c ++上调用析构函数?

时间:2014-09-16 09:13:53

标签: c++

假设我们使用构造函数Def定义了类Def(int)

如果我们有以下代码:

{
   Def obj; //calls default constructor
   Def obj1(3); //calls Def(int) constructor

   obj=Def(3);
   /*calls Def(int) constructor to create temporary on the right side,
     calls assignment   operator (provided by C++) and finally calls
     destructor to destroy temporary object*/

   Def obj2=Def(7);
   /*I thought that here it would create temporary on the right side,
     than call copy constructor and call destructor to destroy temporary object,
     but this doesn't happen. Destructor is not called after expression is
     finished in this scenario.*/    
}

有人可以解释为什么在最后一个表达式中没有调用析构函数吗? 谢谢!

0 个答案:

没有答案