MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / main

Function main

base_code/explicit.cpp:28–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26void doB(B a) {}
27
28int main()
29{
30 A a1(1); // OK:直接初始化
31 A a2 = 1; // OK:复制初始化
32 A a3{ 1 }; // OK:直接列表初始化
33 A a4 = { 1 }; // OK:复制列表初始化
34 A a5 = (A)1; // OK:允许 static_cast 的显式转换
35 doA(1); // OK:允许从 int 到 A 的隐式转换
36 if (a1) {
37 cout << "A True!\n";
38 } // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
39 bool a6(a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
40 bool a7 = a1; // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
41 bool a8 = static_cast<bool>(a1); // OK :static_cast 进行直接初始化
42
43 B b1(1); // OK:直接初始化
44// B b2 = 1; // 错误:被 explicit 修饰构造函数的对象不可以复制初始化
45 B b3{ 1 }; // OK:直接列表初始化
46// B b4 = { 1 }; // 错误:被 explicit 修饰构造函数的对象不可以复制列表初始化
47 B b5 = (B)1; // OK:允许 static_cast 的显式转换
48// doB(1); // 错误:被 explicit 修饰构造函数的对象不可以从 int 到 B 的隐式转换
49 if (b1) {
50 cout << "B True!\n";
51 } // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换
52 bool b6(b1); // OK:被 explicit 修饰转换函数 B::operator bool() 的对象可以从 B 到 bool 的按语境转换
53// bool b7 = b1; // 错误:被 explicit 修饰转换函数 B::operator bool() 的对象不可以隐式转换
54 bool b8 = static_cast<bool>(b1); // OK:static_cast 进行直接初始化
55
56}

Callers

nothing calls this directly

Calls 1

doAFunction · 0.85

Tested by

no test coverage detected