首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制构造函数、析构函数和临时构造器

复制构造函数、析构函数和临时构造器
EN

Stack Overflow用户
提问于 2015-09-09 13:23:48
回答 2查看 322关注 0票数 0

我编写这个类是为了测试默认构造函数、复制构造函数、赋值操作符和析构函数的行为:

代码语言:javascript
复制
#include <iostream>

class Test {

  public:
    Test();

    Test(const Test&);

    ~Test();

    Test &operator=(const Test&);

  private:
    static int count;
    int label;

};

Test::Test() : label(count++) 
{
  std::cout<<"constructor of "<<label<<std::endl;
}

Test::Test(const Test &other) : label(count++)
{
  std::cout<<"copy-constructor of "<<label<<std::endl;
}

Test::~Test()
{
  std::cout<<"destructor of "<<label<<std::endl;
}

Test &Test::operator=(const Test &other)
{
  std::cout<<"copy assignment operator of "<<label<<std::endl;
}

int Test::count=0;

我在不同的上下文中使用了这个类来深入理解如何和何时调用每个函数:我期望注释中的行为:

代码语言:javascript
复制
#include "Test.h"



// argument passed and returned by reference 
const Test &funct_by_ref(const Test &ref)
{
  return ref;
}

// argument passed and returned by value
// call copy constructor to initialize parameter
Test funct_by_val(Test val)
{
  // calls copy constructor to initialize temporary
  return val;
}  // exits local scope,val is destroyed,calls val destructor 



int main()
{
  // creates a temporary,calls 0 default constructor 
  Test();  // the temporary is destroyed at the end of the expression that created it
           // calls 0 destructor
  // creates a temporary (calls 1 default constructor) and calls 2 copy constructor 
  Test t2=Test(); // same as Test t2((Test()));
  std::cout<<"--------------------------------"<<std::endl;
  // calls 3 copy constructor
  Test t3=t2;
  // calls 4 default constructor
  Test t4;
  {
    // calls 5 copy constructor
    Test t5(t4);
  } // local scope,t5 is destroyed,calls 5 destructor
  // calls 4 assignment operator 
  t4=t2;
  std::cout<<"-------------------------------"<<std::endl;
  // nothing happens here
  funct_by_ref(t4);
  std::cout<<"-------------------------------"<<std::endl;
  // calls copy constructor twice 6,7
  funct_by_val(t4);
  // temporary is destroyed at the end of the expression,calls destructor
}

但是,我得到了以下输出:

代码语言:javascript
复制
constructor of 0
destructor of 0
constructor of 1
------------------------
copy-constructor of 2
constructor of 3
copy-constructor of 4
destructor of 4
copy assignment operator of 3
--------------------------
---------------------------
copy-constructor of 5
copy-constructor of 6
destructor of 6
destructor of 5
destructor of 3
destructor of 2
destructor of 1

一切都很好,直到第一次-它似乎跳过了创建一个对象(我想到了初始化t2的临时用法,因为它不是在该行之后被销毁的),所以计数就结束了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-09 13:42:07

代码语言:javascript
复制
Test t2=Test(); // same as Test t2((Test()));

在这里,编译器是(并且可以) 优化副本,并将具有与执行相同的效果:

代码语言:javascript
复制
Test t2;
票数 3
EN

Stack Overflow用户

发布于 2015-09-09 13:45:29

该标准有具体规定,允许--但不要求--实现删除(即省略创建)临时程序,如果检测它们存在的唯一方法是跟踪构造函数和析构函数调用。

您所看到的差异是因为您预期的行为是基于临时人员的创建和破坏,而编译器正在选择不创建那些临时人员。请注意,暂时执行的决定是不同的,这取决于实现(并且经常受到优化设置的影响)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32480806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档