• c/c++拷贝构造函数和关键字explicit


    c/c++拷贝构造函数和关键字explicit

    关键字explicit

    修饰构造方法的关键字,加上了,就告诉编译器,不可以隐式初始化对象;不加就可以隐式初始化对象;
    下面的代码是可以正常编译执行的,但是加了关键字explicit,编译就会错我,因为Test t = 100;是隐式初始化对象,但是如果加上强制类型转换后,就不会有错误了。
    强制类型转换:Test t = (Test)100;

    class Test{
    public:
      Test(int d):data(d){//explicit 
        cout << "C:" << this << endl;
      }
    }
    int main(){
      Test t = 100;
    }
    
    • 拷贝构造函数如果加上了explicit,下面的语句就无法编译通过;不加可以。
    #include <iostream>
    using namespace std;
    class Test{
    public:
      Test(){}
      //拷贝构造函数
      explicit Test(const Test &t){
        cout << "in copy" << endl;
        data = t.data;
      }
      int getData(){
        return data;
      }
    private:
      int data;
    };
    void test(Test x){
    
    }
    int main(){
      Test t1;
      Test t2(t1);//由于是显式调用拷贝构造函数,所以编译过
      //Test t3 = t2;//由于是隐式调用拷贝构造函数,所以编译不过
      //test(t2);//由于是隐式调用拷贝构造函数,所以编译不过
    }
    
    • 触发拷贝构造函数的4种方式

    ​ 1,Test t2(t1);//调用拷贝构造函数

    ​ 2,声明的同时就赋值Test t3 = t2会调用拷贝构造函数;但是注意下面这种不会调用拷贝构造函数。

    ​ Test t3;

    ​ t3 = t2;//会调用=的重载方法

    ​ 3,方法的参数是对象类型test(t2);

    ​ 4,方法的返回值是对象类型。原因:对象tmp在方法结束后就被释放掉了,要返回到函数外,必须要复制tmp.

    但是用gdb看了一下在return处并没有调用拷贝构造函数,所以test方法结束后,tmp也没有被释放,调用test方法的t5的内存地址和tmp是一样的。个人猜测:老版本的gcc编译器可能会在return处调用拷贝构造函数,但是新的编译器(gcc 4.8.5-20)为了提高效率,避免了一次多余的拷贝。

    void test(Test x){//进入函数的时点会调用拷贝构造函数
      int value;
      value = x.getData();
      Test tmp(value);
      return tmp;//return的时点会调用拷贝构造函数
    }
    Test t5 = test(t1);
    

    一个注意点,拷贝构造函数的参数最好用const限定,不然下面的代码编译不过(gcc 4.8.5-20)

    #include <iostream>
    using namespace std;
    
    class Test{
    public:
      Test(int d = 0):data(d){
        cout << "C:" << d << " " << this << endl;
      }
      Test(Test &t){
        cout << "Copy:" << t.data << " " << this << endl;
        data = t.data;
      }
    
      Test& operator = (const Test &t){
        cout << "Assign:" << this << " = " << &t << endl;
        if(this != &t){
          data = t.data;
        }
        return *this;
        }
      ~Test(){
        cout << "F:" << this->data << "->" << this << endl;
      }
      int getData()const{
        return data;
      }
    private:
      int data;
    };
    
    
    Test fun(Test &x){
      int value = x.getData();
      Test tmp(value);
      return tmp;
    
    }
    
    int main(){
      Test t1(100); 
      //编译不过,因为拷贝构造函数的参数没有用const限制
      Test t2 = fun(t1);
    
      return 0;
    }
    
  • 相关阅读:
    CentOS7怎样安装Nginx1.12.2
    CentOS7怎样安装MySQL5.7.22
    CentOS7怎样安装Java8
    CentOS安装JMeter
    CentOS安装nmon
    Unsupported major.minor version 51.0
    ssh问题_2
    数据库索引
    Name node is in safe mode.
    hadoop节点之间通信问题
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9491296.html
Copyright © 2020-2023  润新知