C++拷贝构造函数的学习(一)

     拷贝构造函数:功能:使用一个已经存在的对象来初始化一个新的同一类型的对象

 
     拷贝构造函数的声明:只有一个参数,并且 参数为该对象的引用
     如果类中没有说明拷贝构造函数,则系统自动生成一个缺省复制构造函数,作为该类的公有成员。
     拷贝函数的参数必须是对象引用。
 
     在C++中,下面三种对象需要调用拷贝构造函数:
      1) 一个 对象作为 函数参数,以 值传递的方式 传入函数体;
      2) 一个 对象作为 函数返回值,以 值传递的方式从函数 返回
      3) 一个 对象用于给 另一个对象进行 初始化(常称为复制初始化);
 
    1.当函数的 形参类的对象,调用函数时,进行形参与实参结合时使用,这时要在内存新建立一个 局部对象,并把 实参拷贝到新的对象中,理所当然也要调用拷贝构造函数。
    2.当函数的 返回值是类的对象,函数执行完成返回调用者时使用。理由也是要建立一个临时对象中,再返回调用者。为什么不直接用要返回的局部对象呢?因为 局部对象在离开建立它的函数时就消亡了,不可能在返回调用函数后继续生存,所以处理这种情况时,编译系统会在调用函数的表达式中创建一个 无名临时对象,该临时对象在生存周期值在函数调用处的表达式中。
    3.所谓return对象,实际上是调用拷贝构造函数把该对象的值拷入 临时对象,如果返回的是变量,处理过程类似,只是不用调用构造函数。
    4. 拷贝构造函数的另一种调用:当对象直接作为 参数 传给函数时,函数将建立对象的 临时拷贝 ,这个拷贝过程也将调用拷贝构造函数。
 
 Test.h
 
 
  
  1. # ifndef _TEST_H_ 
  2. # define _TEST_H_ 
  3.  
  4. class Test 
  5. public
  6.     Test(); 
  7.     Test(int num); 
  8. //拷贝函数的参数必须是对象引用 
  9.     Test(const Test& other); 
  10.     ~Test(); 
  11.  
  12.     Test& operator = (const Test& other); 
  13.  
  14.     void Display(); 
  15.  
  16. private
  17.     int num_; 
  18. }; 
  19.  
  20. # endif //_TEST 
Test.cpp
 
 
  
  1. # include "Test.h" 
  2. # include <iostream> 
  3. using namespace std; 
  4.  
  5. Test::Test():num_(0) 
  6.     cout << num_ << endl; 
  7.  
  8. Test::Test(int num):num_(num) 
  9.     cout << num_<<endl; 
  10.  
  11.  
  12. Test::~Test() 
  13.     cout << "Destory " << num_ << endl; 
  14.  
  15. Test::Test(const Test& other):num_(other.num_) 
  16.     cout <<"Inilializing with other " << endl; 
  17.  
  18. Test& Test::operator=(const Test& other) 
  19.     cout << "Test::operator" << endl; 
  20.     if(this == &other) 
  21.     { 
  22.         return *this//t = t; 直接返回 
  23.     } 
  24.     num_ = other.num_; 
  25.      
  26.     return *this;//返回自身 
  27. void Test::Display() 
  28.     cout << num_ << endl; 
main.cpp
 
 
  
  1. # include "Test.h" 
  2. # include <iostream> 
  3. using namespace std; 
  4.  
  5. int main(void
  6.     Test t(10);//调用带一个参数的构造函数    
  7. //t对象初始化t2对象,这时会调用拷贝构造函数 
  8.     Test t2 = t; //此时=不是赋值运算符,而是特殊的解释 
  9.  
  10.     return 0; 
 
运行结果:

 

    下面用实例讲解在什么情况下会调用拷贝构造函数,什么情况下不会调用拷贝构造函数

Test.h

 
 
  
  1. # ifndef _TEST_H_ 
  2. class Test 
  3. public
  4.     Test(); 
  5.     Test(int num); 
  6.     //拷贝构造函数 
  7.     Test(const Test&);  
  8.     ~Test(); 
  9.      
  10.     Test& operator=(const Test& other); 
  11.  
  12.     void Display(); 
  13.  
  14. private
  15.     int num_; 
  16. }; 
  17.  
  18. # endif //_TEST_H_ 
Test.cpp
 
 
  
  1. #include "Test.h" 
  2. # include <iostream> 
  3. using namespace std; 
  4.  
  5. Test::Test(void):num_(0) 
  6.     cout << num_ << endl; 
  7.  
  8. Test::Test(int num):num_(num) 
  9.     cout << num_<<endl; 
  10.  
  11. Test::~Test(void
  12.     cout << "Deatory ..." << endl; 
  13.  
  14. Test::Test(const Test& other):num_(other.num_) 
  15.     cout << "Iniliazing with other..." << endl; 
  16.  
  17. Test& Test::operator=(const Test& other) 
  18.     cout << "Test::operator "<< endl; 
  19.     if(this == &other) 
  20.     { 
  21.         return *this
  22.     } 
  23.     num_ = other.num_; 
  24.  
  25.     return *this
  26.  
  27. void Test::Display() 
  28.     cout << num_ << endl; 
main.cpp
 
 
  
  1. # include "Test.h" 
  2. # include <iostream> 
  3. using namespace std; 
  4.  
  5. void TestFun(const Test t) 
  6.      
  7. //因为是引用不会构造对象分配内存的 
  8. void TestFun_reference_parameter(const Test& t) 
  9.  
  10. const Test& TestFun_const_return_reference (const Test& t) 
  11.     return t; 
  12. //返回对象的时候,要创建一个临时对象 
  13. Test TestFun_return_object(const Test& t) 
  14. {//进行复制操作 
  15.     return t;//临时对象  
  16.  
  17. int main(void
  18.     Test t(10);//调用拷贝构造函数 
  19.     cout << "." << endl; 
  20.     TestFun(t);//调用拷贝构造函数 
  21.     cout << ".." << endl; 
  22.     因为是引用不会构造对象分配内存的 
  23.     TestFun_reference_parameter(t); 
  24.  
  25.     Test t2 = t;//调用拷贝构造函数 
  26.     cout << "..." << endl; 
  27.  
  28.     TestFun_return_object(t); //调用拷贝构造函数 
  29.     t = TestFun_return_object(t);//引用的话,临时对象也不会马上被销毁 
  30.     cout << "...." << endl; 
  31.          
  32.     //TestFun(t); 
  33.    Test t4 = TestFun_const_return_reference(t);//调用拷贝构造函数 ,因为返回的是t 
  34.  
  35.     return 0; 
运行结果:
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值