Constructor函数中的explicit

来源:互联网 发布:权力的游戏 mac 编辑:程序博客网 时间:2024/05/18 01:30
class T {private:int a;double b;string c;public:T() :a(1), b(1), c("none") {}T(int n) :a(n) {}T(double d) :b(d) {}T(string s) : c(s) {}void print() {cout << a << endl;cout << b << endl;cout << c << endl;}};int main() {T x;x = 10.8; //this line will invokes from T(double d) :b(d) {}, and creates a temporary class T object, then x will be memberwise-copied by this object, all values of x from defination will be replaced.x.print();system("pause");return 0;}


class T {private:int a;double b;string c;public:T() :a(1), b(1), c("none") {}T(int n) :a(n) {}explicit T(double d) :b(d) {}T(string s) : c(s) {}void print() {cout << a << endl;cout << b << endl;cout << c << endl;}};int main() {T x;x = 10.8; // invokes from T(int n) :a(n) {}, 10.8 -> 10.x.print();system("pause");return 0;}


class T {private:int a;double b;string c;public:T() :a(1), b(1), c("none") {}explicit T(int n) :a(n) {}explicit T(double d) :b(d) {}T(string s) : c(s) {}void print() {cout << a << endl;cout << b << endl;cout << c << endl;}};int main() {T x;x = 10.8; // error, because explicit turns off implicit conversions.x.print();system("pause");return 0;}


                                             
0 0
原创粉丝点击