C++面向对象(5)

来源:互联网 发布:做淘宝电脑配置 编辑:程序博客网 时间:2024/06/08 11:35

构造函数的使用

以下代码中的输出语句为0吗?为什么?

[cpp] view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. struct CLS  
  6. {  
  7.     int m_i;  
  8.     CLS(int i):m_i(i){ }  
  9.     CLS()  
  10.     {  
  11.         CLS(0);  
  12.     }  
  13. };  
  14.   
  15. int main()  
  16. {  
  17.     CLS obj;  
  18.     cout<<obj.m_i<<endl;  
  19.       
  20.     return 0;  
  21. }  
在代码第11行,不带参数的构造函数调用了带参数的构造函数。这种调用往往被很多人误解,其实是不行的,而且往往会有副作用。可以加几条打印语句测试一下:

[cpp] view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. struct CLS  
  6. {  
  7.     int m_i;  
  8.     CLS(int i):m_i(i)  
  9.     {  
  10.         cout<<"CLS():this= "<<this<<endl;   
  11.     }  
  12.     CLS()  
  13.     {  
  14.         CLS(0);  
  15.         cout<<"CLS():this= "<<this<<endl;   
  16.     }  
  17. };  
  18.   
  19. int main()  
  20. {  
  21.     CLS obj;  
  22.         cout<<"&obj= "<<&obj<<endl;  
  23.        cout<<obj.m_i<<endl;  
  24.       
  25.     return 0;  
  26. }  
程序执行结果:

[cpp] view plain copy
  1. CLS():this= 0xbffa176c  
  2. CLS():this= 0xbffa179c  
  3. &obj= 0xbfe7391c  
  4. 7823348  
  可以看到,在带参数的构造函数里打印出来的对象地址和对象obj的地址不一致。实际上,代码14行的调用只是在栈上生成了一个临时对象,对于自己本身毫无影响。还可以发现,构造函数的相互调用引起的后果不是死循环,而是栈溢出。


构造函数explicit与普通构造函数的区别

explicit构造函数是用来防止隐式转换的。

[cpp] view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Test1 {  
  6. public:  
  7.     Test1(int n) { num = n; }                       //普通构造函数  
  8. private:  
  9.     int num;  
  10. };  
  11.   
  12. class Test2 {  
  13. public:  
  14.     explicit Test2(int n) { num = n; }              //explicit(显示)构造函数  
  15. private:  
  16.     int num;  
  17. };  
  18.   
  19. int main()  
  20. {  
  21.     Test1 t1 = 12;                                  //隐式调用其构造函数,成功  
  22.     Test2 t2 = 12;                                  //编译错误,不能隐式调用其构造函数  
  23.     Test2 t3(12);                                   //显示调用成功  
  24.       
  25.     return 0;  
  26. }  
  Test1的构造函数带一个int型参数,代码第21行会隐式转换成调用Test1的构造函数。而Test2的构造函数被声明为explicit(显示),这表示不能通过隐式转换来调用这个构造函数,因此22行编译错误。


explicit构造函数的作用

[cpp] view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Number {  
  6. public:  
  7.     string type;  
  8.     Number():type("void"){}  
  9.     explicit Number(short):type("short") {}  
  10.     Number(int):type("int"){ }  
  11. };  
  12.   
  13. void show(const Number& n) { cout<<n.type<<endl; }  
  14.   
  15. int main()  
  16. {  
  17.     short s=42;  
  18.     show(s);  
  19.       
  20.     return 0;  
  21. }  
show()函数的参数类型是Number类对象的引用,18行调用show(s)时采取了以下所示的步骤:

(1)show(s)中的s为short类型,其值为42,因此首先检查参数为short的构造函数能否被隐式转换。由于第9行的构造函数被声明为explicit(显示调用),因此不能隐式转换。于是进行下一步。

(2)42自动转换为int型。

(3)检查参数为int的构造函数能否被隐式转换。由于第10行参数为int的构造函数吗,没有被声明为显示调用,因此此构造函数临时构造出一个临时对象。

(4)打印上一步临时对象type的成员,即“int”

原创粉丝点击