From 71d83ce4669754bacdf4a51071cbc40ca4e72706 Mon Sep 17 00:00:00 2001 From: light-city <455954986@qq.com> Date: Tue, 6 Aug 2019 14:50:15 +0800 Subject: [PATCH] update dir --- README.md | 3 ++- explicit/README.md | 10 ++++++++++ explicit/explict | Bin 0 -> 9288 bytes explicit/explict.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 explicit/README.md create mode 100755 explicit/explict create mode 100644 explicit/explict.cpp diff --git a/README.md b/README.md index 085b490..64156ea 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,8 @@ - [x] [struct那些事](./struct) - [x] [struct与class那些事](./struct_class) - [x] [union那些事](./union) -- [x] [c实现c++多态](./c_poly) +- [x] [c实现c++多态那些事](./c_poly) +- [x] [explicit那些事](./explicit) 代码运行: 全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行! diff --git a/explicit/README.md b/explicit/README.md new file mode 100644 index 0000000..8fbe13f --- /dev/null +++ b/explicit/README.md @@ -0,0 +1,10 @@ +# explicit(显式)关键字那些事 + +- explicit 修饰构造函数时,可以防止隐式转换和复制初始化 +- explicit 修饰转换函数时,可以防止隐式转换,但按语境转换除外 + + +代码参见:[./explict.cpp](./explicit.cpp) + +参考链接: +> https://stackoverflow.com/questions/4600295/what-is-the-meaning-of-operator-bool-const diff --git a/explicit/explict b/explicit/explict new file mode 100755 index 0000000000000000000000000000000000000000..888e363d011400d15fd013a52ce83edebb45c046 GIT binary patch literal 9288 zcmeHNeQaCR6~DHVH2uJFp|H{->OniwvY5C{Lka_ICuv{0^n)gajuBoQ+i4zk?8<&F zX{BzU6sYOSqQSII+O#4z1RB~pfwYcIZK@&)+WvrT@DGIrM!S@cQ3O<(R+|0JefK;+ zzc>tO+F#qP{LVeU^L4-9yC?m2d*^De$0N9S#m$1a#+rzPtfvrruaK<5TEt8d5sSnX zq6SDcei11lYSoxdDA!DDm7Wi@3cnVw1+;i0vci#w% zhwJj!yz=v3K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hW#3Cmj>35qrjJ7Av=7!qq;^m> zh}+99-29JS_WXU>sO&eXb-?x8YiD^147H!fB&{5pu_Y!3E?asr*_XYmDx_i9b z_OHU0?gVVl_vBi2A#m%v`L|$8O=Nq%PY1rB{b|_JRI@$jh4}wHjH&*sJgsEdxfz05QH&}DDm<5hqN_=cxL#o literal 0 HcmV?d00001 diff --git a/explicit/explict.cpp b/explicit/explict.cpp new file mode 100644 index 0000000..f659635 --- /dev/null +++ b/explicit/explict.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +struct A +{ + A(int) { } + operator bool() const { return true; } +}; + +struct B +{ + explicit B(int) {} + explicit operator bool() const { return true; } +}; + +void doA(A a) {} + +void doB(B b) {} + +int main() +{ + A a1(1); // OK:直接初始化 + A a2 = 1; // OK:复制初始化 + A a3{ 1 }; // OK:直接列表初始化 + A a4 = { 1 }; // OK:复制列表初始化 + A a5 = (A)1; // OK:允许 static_cast 的显式转换 + doA(1); // OK:允许从 int 到 A 的隐式转换 + if (a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 + bool a6(a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 + bool a7 = a1; // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换 + bool a8 = static_cast(a1); // OK :static_cast 进行直接初始化 + + B b1(1); // OK:直接初始化 +// B b2 = 1; // 错误:被 explicit 修饰构造函数的对象不可以复制初始化 + B b3{ 1 }; // OK:直接列表初始化 +// B b4 = { 1 }; // 错误:被 explicit 修饰构造函数的对象不可以复制列表初始化 + B b5 = (B)1; // OK:允许 static_cast 的显式转换 +// doB(1); // 错误:被 explicit 修饰构造函数的对象不可以从 int 到 B 的隐式转换 + if (b1); // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换 + bool b6(b1); // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换 +// bool b7 = b1; // 错误:被 explicit 修饰转换函数 B::operator bool() 的对象不可以隐式转换 + bool b8 = static_cast(b1); // OK:static_cast 进行直接初始化 + + return 0; +}