构造函数的使用

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

    #include <iostream>  
      
    using namespace std;  
      
    struct CLS  
    {  
        int m_i;  
        CLS(int i):m_i(i){ }  
        CLS()  
        {  
            CLS(0);  
        }  
    };  
      
    int main()  
    {  
        CLS obj;  
        cout<<obj.m_i<<endl;  
          
        return 0;  
    }  

在代码第11行,不带参数的构造函数调用了带参数的构造函数。这种调用往往被很多人误解,其实是不行的,而且往往会有副作用。可以加几条打印语句测试一下:

    #include <iostream>  
      
    using namespace std;  
      
    struct CLS  
    {  
        int m_i;  
        CLS(int i):m_i(i)  
        {  
            cout<<"CLS():this= "<<this<<endl;   
        }  
        CLS()  
        {  
            CLS(0);  
            cout<<"CLS():this= "<<this<<endl;   
        }  
    };  
      
    int main()  
    {  
        CLS obj;  
            cout<<"&obj= "<<&obj<<endl;  
           cout<<obj.m_i<<endl;  
          
        return 0;  
    }  
程序执行结果:
CLS():this= 0xbffa176c  
CLS():this= 0xbffa179c  
&obj= 0xbfe7391c  
7823348

可以看到,在带参数的构造函数里打印出来的对象地址和对象obj的地址不一致。实际上,代码14行的调用只是在栈上生成了一个临时对象,对于自己本身毫无影响。还可以发现,构造函数的相互调用引起的后果不是死循环,而是栈溢出。


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

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

    #include <iostream>  
      
    using namespace std;  
      
    class Test1 {  
    public:  
        Test1(int n) { num = n; }                       //普通构造函数  
    private:  
        int num;  
    };  
      
    class Test2 {  
    public:  
        explicit Test2(int n) { num = n; }              //explicit(显示)构造函数  
    private:  
        int num;  
    };  
      
    int main()  
    {  
        Test1 t1 = 12;                                  //隐式调用其构造函数,成功  
        Test2 t2 = 12;                                  //编译错误,不能隐式调用其构造函数  
        Test2 t3(12);                                   //显示调用成功  
          
        return 0;  
    }  

Test1的构造函数带一个int型参数,代码第21行会隐式转换成调用Test1的构造函数。而Test2的构造函数被声明为explicit(显示),这表示不能通过隐式转换来调用这个构造函数,因此22行编译错误。


explicit构造函数的作用

    #include <iostream>  
      
    using namespace std;  
      
    class Number {  
    public:  
        string type;  
        Number():type("void"){}  
        explicit Number(short):type("short") {}  
        Number(int):type("int"){ }  
    };  
      
    void show(const Number& n) { cout<<n.type<<endl; }  
      
    int main()  
    {  
        short s=42;  
        show(s);  
          
        return 0;  
    }  

show()函数的参数类型是Number类对象的引用,18行调用show(s)时采取了以下所示的步骤:

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

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

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

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


Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐