目录
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 中。
explicit作用:
在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显式的方式进行类型转换。
explicit使用注意事项:
(1)explicit 关键字只能用于类内部的构造函数声明上。
(2) explicit 关键字作用于单个参数的构造函数。
* 在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换
例子
#include
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<
上面三行的报错信息:(这个是我原来在另外一个账号写的博客,现在账号转移到这个了)

explicit可以抑制内置类型隐式转换,所以在类的构造函数中,最好尽可能多用explicit关键字,防止不必要的隐式转换。
上一篇:[附源码]java毕业设计在线二手车交易信息管理系统
下一篇:忠正不渝什么意思