zoukankan      html  css  js  c++  java
  • 28)拷贝构造函数

    1)由已存在的对象,创建新对象,也就是说新对象,不由构造器来构造,而是由拷贝构造器来完成。

    2)拷贝构造器的格式:
      

    1 class 类名
    2 {
    3    类名(const 类名 & another)
    4           {
    5             拷贝构造体
    6            } 
    7 
    8 }

    3)拷贝构造函数,为啥是只读的,因为我仅仅是用它来初始化自己,我不会改变那个对象的值,所以加一个const保护起来

    4)

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test
     5 {
     6 public:
     7     Test()
     8     {
     9         m_x=1000;
    10         m_y=100090;
    11     }
    12     Test(int x,int y)
    13     {
    14     m_x=x;
    15     m_y=y;
    16     
    17     }
    18     void print_T()
    19     {
    20         cout<<"x="<<m_x<<endl<<"y="<<m_y<<endl;
    21     }
    22     //拷贝构造函数,为啥是只读的,因为我仅仅是用它来初始化自己,我不会改变那个对象的值,所以加一个const保护起来
    23     Test(const Test &another)
    24     {
    25         m_x=another.m_x;
    26         m_y=another.m_y;
    27     }
    28     private:
    29     int m_x;
    30     int m_y;
    31 };
    32 
    33 int main()
    34 {
    35     Test t1;
    36     Test t2(t1);//就是我初始化t2,叫t1给我初始化,但是,这个函数怎么写呢,就是编写一个拷贝函数。
    37     //居然能编译通过,就说明,系统有一个默认这种接口,所以 才没有报错
    38 
    39 
    40     //写一个显式的拷贝构造函数
    41     //啥是 拷贝对象,就是用和我同一个类的对象来初始化自己。
    42 
    43     t2.print_T();
    44 
    45 
    46 
    47 
    48 
    49 
    50 
    51 
    52     return 0;
    53 }

    5)即使我不写那个拷贝构造函数  其实用另一种方式初始化 也是可以的

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test
     5 {
     6 public:
     7     Test()
     8     {
     9         m_x=1000;
    10         m_y=100090;
    11     }
    12     Test(int x,int y)
    13     {
    14     m_x=x;
    15     m_y=y;
    16     
    17     }
    18     void print_T()
    19     {
    20         cout<<"x="<<m_x<<endl<<"y="<<m_y<<endl;
    21     }
    22 
    23 
    24     /*Test(const Test &another)
    25     {
    26         m_x=another.m_x;
    27         m_y=another.m_y;
    28     }*/
    29 
    30     private:
    31     int m_x;
    32     int m_y;
    33 };
    34 
    35 int main()
    36 {
    37     Test t1;
    38     //Test t2(t1);
    39 
    40     Test t2=t1;//其实下面这个和上面的那个是一样的效果
    41     t2.print_T();
    42     return 0;
    43 }

    6)但是,如果我的代码这样写,就是调用的是Test的拷贝构造函数

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test
     5 {
     6 public:
     7     Test()
     8     {
     9         m_x=1000;
    10         m_y=100090;
    11     }
    12     Test(int x,int y)
    13     {
    14     m_x=x;
    15     m_y=y;
    16     
    17     }
    18     void print_T()
    19     {
    20         cout<<"x="<<m_x<<endl<<"y="<<m_y<<endl;
    21     }
    22     private:
    23     int m_x;
    24     int m_y;
    25 };
    26 
    27 int main()
    28 {
    29     Test t1;
    30     Test t2;
    31     t2=t1;//这个调用的不是Test的拷贝操作符函数,而是Test的赋值操作符函数(就是那个操作符函数重载)
    32     t2.print_T();
    33 
    34     return 0;
    35 }

       调的是  

    1 void  operator=(const Test &another)
    2 {
    3     m_x=another.m_x;
    4     m_y=another.m_y;
    5 }

     8) 当我一个函数返回一个对象,如果在函数外部没有任何变量去接收它,这个对象将不会再被使用,(找不到),编译会直接将这个匿名对象回收掉,而不是等待函数执行完毕再回收。

    9)

        

          这个func2()返回一个对象,因为 return  Test1,(其实仅仅Test1的值,所以  这个函数返回了,就作为一个匿名对象出现)

          这样,那个匿名对象有了名字,就不会被回收了,然后  就不会调用析构了。

         

          不懂  可以看  台式机的:

             C:Documents and SettingsAdministrator桌面C++基础教程完整版视频2_C++基础day033_视频  --第七个
     

    10)

  • 相关阅读:
    Ext学习之路——Ext.application
    MVVM模式理解
    MVC和MVVM的区别
    如何才能通俗易懂的解释js里面的‘闭包’?
    【移动端】用Cordova将vue项目打包成app
    【vue源码】js中的with语句
    【移动端】移动端点击可点击元素时,出现蓝色默认背景色
    【人脸识别】纯前端实现人脸融合-调用Face++的人脸融合API接口实现
    new Function()语法
    Element-ui 自定义下拉框,实现选择图片并且回显图片
  • 原文地址:https://www.cnblogs.com/xiaoyoucai/p/8183225.html
Copyright © 2011-2022 走看看