C++ explicit关键字

C++ explicit关键字
 1 #include <iostream>
 2 using namespace std;
 3 class Test1
 4 {
 5 public:
 6     int m;
 7     Test1(int n) { num = n; } //普通构造函数
 8     void Show()
 9     {
10         cout << "private num = " << num <<endl;
11     }
12 private:
13     int count;
14     int num;
15     int n;
16 };
17 class Test2
18 {
19 public:
20     explicit Test2(int n) { num = n; } //explicit(显式)构造函数
21 private:
22     int num;
23 };
24 int main()
25 {
26     Test1 t1 = 12; //隐式调用其构造函数, 成功
27     t1.Show();
28     
29     //Test2 t2 = 12; //编译错误,不能隐式调用其构造函数
30     Test2 t3(12); //显式调用成功
31     return 0;
32 }
C++ explicit关键字

C++ explicit关键字,布布扣,bubuko.com

C++ explicit关键字

上一篇:性能优化之C++ Profiler


下一篇:Python——字符串