C++ explicit关键字

时间:2014-03-05 15:44:30   收藏:0   阅读:553
bubuko.com,布布扣
 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 }
bubuko.com,布布扣

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

原文:http://www.cnblogs.com/autumoonchina/p/3581080.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!