c++拷贝构造函数

一、整体代码

  Test.h

  [cpp]

  #ifndef _TEST_H_

  #define _TEST_H_

  class Test

  {

  public:

  // 如果类不提供任何一个构造函数,系统将为我们提供一个不带参数的

  // 默认的构造函数

  Test();

  explicit Test(int num);

  Test(const Test& other);

  void Display();

  Test&operator=(const Test& other);

  ~Test();

  public:

  int num_;

  };

  #endif// _TEST_H_

  Test.cpp

  [cpp]

  #include"Test.h"

  #include<iostream>

  usingnamespace std;

  // 不带参数的构造函数称为默认构造函数

  Test::Test() : num_(0)

  {

  //num_ = 0;

  cout《"Initializing Default"《endl;

  }

  Test::Test(int num) : num_(num)

  {

  //num_ = num;

  cout《"Initializing "《num_《endl;

  }

  Test::Test(const Test& other) : num_(other.num_)

  {

  //num_ = other.num_;

  cout《"Initializing with other "《num_《endl;

  }

  Test::~Test()

  {

  cout《"Destroy "《num_《endl;

  }

  void Test::Display()

  {

  cout《"num="《num_《endl;

  }

  Test& Test::operator=(const Test& other)

  {

  cout《"Test::operator="《endl;

  if (this==&other)

  return*this;

  num_ = other.num_;

  return*this;

  }

  01.cpp

  [cpp]

  01.cpp

  #include"Test.h"

  #include<iostream>

  usingnamespace std;

  Test TestFun(Test t)

  {

  t.num_=12;

  return t;

  }

  Test& TestFun2(Test t)

  {

  return t;

  }

  Test TestFun3(Test& t)

  {

  return t;

  }

  Test& TestFun4(Test& t)

  {

  //return const_cast<Test&>(t);

  return t;

  }

  int main(void)

  {

  Test t(10);

  Test t2 = TestFun(t);

  cout《"……"《endl;

  return 0;

  }


二、运行结果

  1、Test t2 = TestFun(t);

  (1)Test t(10) :生成一个对象1(num_是10),对应Initializing 10

  (2)把t对象传递到形式参数时:调用拷贝构造函数,由对象1生成了一个对象2(num_是10),之后又改变了num_为12,对应Initializing with other10

  (3)返回值时:调用了拷贝构造函数,由对象2生成了一个对象3(num_是12),对应Initializing with other12

  (4)Test t2 = TestFun(t):t2接管了对象3,不重新申请地址空间

  (5)TestFun返回后:对象2销毁了,对应Destroy 12

  (6)……

  (7)程序结束后:首先销毁听t2(接管了对象1),然后销毁对象1,对应Destroy 12 ,Destroy 10

  TestFun(t) 此时没有对象接管,运行结果如下:

  (1)Test t(10) :生成一个对象1(num_是10),对应Initializing 10

  (2)把t对象传递到形式参数时:调用拷贝构造函数,由对象1生成了一个对象2(num_是10),之后又改变了num_为12,对应Initializing with other10

  (3)返回值时:调用了拷贝构造函数,由对象2生成了一个对象3(num_是12),对应Initializing with other12

  (4)TestFun返回后:对象3销毁,对象2销毁,对应Destroy 12,Destroy 12

  (5)……

  (6)程序结束后:销毁对象1,对应Destroy 10

  2、Test t2 = TestFun2(t);

  (1)Test t(10) :生成一个对象1(num_是10),对应Initializing 10

  (2)把t对象传递到形式参数时:调用拷贝构造函数,由对象1生成了一个对象2(num_是10),对应Initializing with other 10

  (3)Test t2 = TestFun2(t):由对象2生成了对象3(num_是10),对应Initializing with other 10

  (4)TestFun2返回后:对象2销毁,对应Destory 10

  (5)……

  (6)程序结束后:销毁对象3,销毁对象1,对应Destory 10,Destory 10

  3、Test t2 = TestFun3(t);

  (1)Test t(10) :生成一个对象1(num_是10),对应Initializing 10

  (2)返回值时:由对象1生成了对象2(num_是10),对应Initializing with other 10

  (3)Test t2 = TestFun3(t):t2对象接管了对象2

  (4)……

  (5)程序结束后:销毁t2对象,销毁对象1,对应Destroy 10,Destroy 10

  4、Test t2 = TestFun4(t);

  (1)Test t(10) :生成一个对象1(num_是10),对应Initializing 10

  (2)Test t2 = TestFun4(t):由对象1生成了对象t2(num_是10),对应Initializing with other 10

  (3)……

  (4)程序结束后:销毁t2对象,销毁对象1,对应Destroy 10,Destroy 10

  三、什么时候会调用拷贝构造函数

  一个对象初始化另一个全新的对象时,会调用,如Test t;  Test t2 = t;

  Test t ; Test t2; t2=t1;会调用赋值语句


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值