zoukankan      html  css  js  c++  java
  • C++中的explicit

    先看如下例子:

    #include<iostream>
    using namespace std;
    class Number
    {
    public:
        Number(int x):_x(x) 
        {
            cout << "Number(int x):_x(x)" << endl;
        }
    private:
        int _x;
    };
    
    int main(int argc, char* argv[])
    {
        string s = "hello";
        Number num = 123;
        return 0;
    }

    从string和Number字面不难推导出s和num的类型,因此将hello和123赋值给s和num是合理的。

    接下来改变代码:

    #include<iostream>
    using namespace std;
    class A
    {
    public:
        A(int x):_x(x) 
        {
            cout << "A(int x):_x(x)" << endl;
        }
    private:
        int _x;
    };
    
    int main(int argc, char* argv[])
    {
        string s = "hello";
        A a = 123;
        return 0;
    }

    对于A而言,他到底是一个什么类型很难推断出,而将一个整型数字赋值给a,很可能导致理解上的偏差,因为并无这样的构造函数存在,于是在构造函数前加上explicit,避免了编译器的自动隐式类型转换。

     1 #include<iostream>
     2 using namespace std;
     3 class A
     4 {
     5 public:
     6     explicit A(int x):_x(x) 
     7     {
     8         cout << "A(int x):_x(x)" << endl;
     9     }
    10 private:
    11     int _x;
    12 };
    13 
    14 int main(int argc, char* argv[])
    15 {
    16     string s = "hello";
    17     A a(123);
    18     return 0;
    19 }

    再看上述代码,只有显示的调用了A a(123)构造函数才可以创造新的对象,如果此时A a=123将会出错,因为加了explicit,编译器将会静止这种隐式类型转化。

    可见,explicit的作用说白了就是,如果整个类型从字面上很明确可以猜出来,比如string类型,直接将"hello"赋值给s,这并无大碍,也就是这样的赋值也是可以的,如果该类型很不明确,为了避免理解上的困难,通常加上explicit避免这种隐式类型转换。

    explict其他情况:

    explicit只能修饰单参的构造函数,如果多个参数,那么其他的参数必须是默认的:

        A(char c, int x = 1) :_c(c), _x(x) {}
        A(int x, int y = 2, int z = 1) {}
        A(int x):_x(x) {}

    A a='x';

    A x=12;

    A y=321;

  • 相关阅读:
    Error:(3, 32) java: 程序包org.springframework.boot不存在
    Interface IntUnaryOperator
    java “错误:编码GBK 的不可映射字符”
    Spring boot整合Mybatis
    免安装版Mysql安装教程
    Maven中标签的介绍
    Post的Conotent-typeyy引发的有关http的思考
    Git图谱的介绍
    使用stream求数组中的平均值
    RabbitMQ实际使用场景
  • 原文地址:https://www.cnblogs.com/SunShine-gzw/p/15426070.html
Copyright © 2011-2022 走看看