C++中的explicit
2021/10/19 20:39:45
本文主要是介绍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;
这篇关于C++中的explicit的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03如何安装 App 并连接到飞牛 NAS?-icode9专业技术文章分享
- 2024-10-03如何安装飞牛 TV 并连接到影视服务器?-icode9专业技术文章分享
- 2024-10-03如何在PVE和ESXI上安装飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS安装系统异常情况处理-icode9专业技术文章分享
- 2024-10-03飞牛NAS如何创建存储空间?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS硬盘会自动休眠吗?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何安装飞牛影视和创建媒体库?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何为家人朋友开通影视账号?-icode9专业技术文章分享