• C++ explicit 用法


    引言

    本文介绍了 C++ 中 explicit 关键字的用法.

    原文地址: C++ explicit 用法

    欢迎访问我的博客: http://blog.duhbb.com/

    explicit 作用

    在 C++中, explicit 关键字用来修饰类的构造函数, 被修饰的构造函数的类, 不能发生相应的隐式类型转换, 只能以显式的方式进行类型转换.

    explicit 使用注意事项:

    1. explicit 关键字只能用于类内部的构造函数声明上.
    2. explicit 关键字作用于单个参数的构造函数 (多个参数也适用, 但必须只有一个参数需要赋值, 其他的参数有默认值).
    3. 在 C++ 中, explicit 关键字用来修饰类的构造函数, 被修饰的构造函数的类, 不能发生相应的隐式类型转换.
    4. it cannot be used for implicit conversions and copy-initialization

    多个参数的情况

    其实对于上面的第二点我是存疑的, 因为 CPP Reference explicit specifier 中的例子表明 explicit 是可以用于多个参数的.

    例子

    例 1 基础例子

    #include <bits/stdc++.h>
    using namespace std;
    
    
    class Test1 {
    public:
    
        Test1(int n) {
            m_num = n;
        }
    
    private:
        int m_num;
    };
    
    
    class Test2 {
    public:
        explicit Test2(int n) {
            m_num = n;
        }
    
    private:
        int m_num;
    };
    
    int main() {
    
        Test1 t1 = 12;              // 隐式调用构造函数成功
        Test2 t2 = 12;              // 编译错误, 不能隐式调用其构造函数
        Test2 t2(12);               // 显式调用成功
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    例 2 CPP Reference 中的例子

    下面是 CPP Reference explicit specifier 中的例子, 感觉更全面一点:

    struct A
    {
        A(int) { }      // converting constructor
        A(int, int) { } // converting constructor (C++11)
        operator bool() const { return true; }
    };
    
    struct B
    {
        explicit B(int) { }
        explicit B(int, int) { }
        explicit operator bool() const { return true; }
    };
    
    int main()
    {
        A a1 = 1;      // OK: copy-initialization selects A::A(int)
        A a2(2);       // OK: direct-initialization selects A::A(int)
        A a3 {4, 5};   // OK: direct-list-initialization selects A::A(int, int)
        A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
        A a5 = (A)1;   // OK: explicit cast performs static_cast
        if (a1) ;      // OK: A::operator bool()
        bool na1 = a1; // OK: copy-initialization selects A::operator bool()
        bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization
    
    //  B b1 = 1;      // error: copy-initialization does not consider B::B(int)
        B b2(2);       // OK: direct-initialization selects B::B(int)
        B b3 {4, 5};   // OK: direct-list-initialization selects B::B(int, int)
    //  B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int)
        B b5 = (B)1;   // OK: explicit cast performs static_cast
        if (b2) ;      // OK: B::operator bool()
    //  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
        bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    总结

    C++ explicit 还是比较好理解的, 主要就是避免隐式转换, C++ 中的转化真的是太灵活了, 容易搞出 bug 来.

    原文地址: C++ explicit 用法

    欢迎访问我的博客: http://blog.duhbb.com/

  • 相关阅读:
    静图表情包怎么做成动态图?动图表情包制作教程
    C++DAY40
    import机制浅析
    Docker搭建harbor仓库
    Aop天花板
    中文转拼音的方法
    cpp占位参数在重载运算符中的作用
    python机器学习:集成算法与随机森林(5)
    2023蓝帽杯南部赛区半决赛取证复现
    linux中各种最新网卡2.5G网卡驱动,不同型号的网卡需要不同的驱动,整合各种网卡驱动,包括有线网卡、无线网卡、Wi-Fi热点
  • 原文地址:https://blog.csdn.net/ea8d1n3/article/details/125511588