C++ 中explicit的作用及用法

目录

 C++explicit(官网的说法)

C++explicit 清楚的说法(建议英文不好的从这里开始食用哦)

C++explicit使用的例子(建议喜欢自己敲代码实验的从这里开始食用哦)

总结


 C++explicit(官网的说法)

explicit specifier:explicit specifier - cppreference.com

explicit(1)
explicit ( expression )(2)(since C++20)
expression-contextually converted constant expression of type bool


1) Specifies that a constructor or conversion function (since C++11) or deduction guide (since C++17) is explicit, that is, it cannot be used for implicit conversions and copy-initialization.

翻译:指定构造函数或转换函数 (C++11 起) 或推导指南 (C++17 起) 是显式的,也就是说,它不能用于隐式转换和复制初始化。

2) The explicit specifier may be used with a constant expression. The function is explicit if and only if that constant expression evaluates to true.

(since C++20)
翻译:显式说明符可以与常量表达式一起使用。当且仅当该常量表达式的计算结果为真时,该函数才是显式的。 

The explicit specifier may only appear within the decl-specifier-seq of the declaration of a constructor or conversion function (since C++11) within its class definition.

翻译:显式说明符只能出现在其类定义中的构造函数或转换函数(C++11 起)声明的 decl-specifier-seq 中。

C++explicit 清楚的说法(建议英文不好的从这里开始食用哦)


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

explicit使用注意事项:
      (1)explicit 关键字只能用于类内部的构造函数声明上。

      (2) explicit 关键字作用于单个参数的构造函数。

    * 在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换

C++explicit使用的例子(建议喜欢自己敲代码实验的从这里开始食用哦)


例子

#include <bits/stdc++.h>
using namespace std;
class Circle
{
    public:
        Circle(){}
        Circle(double _a):a(_a){}
        Circle(int _b, int _c):b(_b), c(_c){}
 
        Circle(const Circle& A)
        {
            a=A.a; b=A.b; c=A.c;
        }
        void Print()
        {
            cout<<a<<" "<<b<<" "<<c<<endl;
        }
    private:
        double a;
        int b;
        int c;
 
};
class Circle1
{
    public:
        Circle1(){}
        explicit Circle1(double _a):a(_a){}
        explicit Circle1(int _b, int _c):b(_b), c(_c){}
        explicit Circle1(const Circle1& A)
        {
            a=A.a; b=A.b; c=A.c;
        }
        void Print()
        {
            cout<<a<<" "<<b<<" "<<c<<endl;
        }
    private:
        double a;
        int b;
        int c;
 
};
int main()
{
    Circle q1(1);
    Circle w1(2, 3);
    q1.Print();
    w1.Print();
 
    //隐式调用, 不会报错
    Circle q2 = 1;// just like Circle q(1); 调用的是Circle(double _a)
    Circle w2 = 1.0;// just like Circle q(1.0); 调用的是Circle(double _a)
    Circle e = q2;// 调用的是Circle(const Circle& A)
    
    
    //隐式调用,会报错,注意Circle1()的构造函数,都有explicit
    // Circle1 q3 = 1;
    // Circle1 w3 = 1.0;
    // Circle1 e1 = q3;
    // 以上三行都会报错。
 
    //显式调用,都不会有问题
    Circle1 q3(1);
    Circle1 w3(1.0);
    Circle1 e1(q3);
 
}

上面三行的报错信息:(这个是我原来在另外一个账号写的博客,现在账号转移到这个了)

 

  


总结


explicit可以抑制内置类型隐式转换,所以在类的构造函数中,最好尽可能多用explicit关键字,防止不必要的隐式转换。
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值