zoukankan      html  css  js  c++  java
  • [一道搜狗输入法的面试题]C++转换构造函数和类型转换函数

    今天面试遇到一道有关C++转换构造函数的题目,之前经常见到默认构造函数、拷贝构造函数、析构函数,但是从没听说过转换构造函数,隐式转换函数也是一样,C++的确是够博大精深的,学习之路很长啊!

    其实我们已经在C/C++中见到过多次标准类型数据间的转换方式了,这种形式用于在程序中将一种指定的数据转换成另一指定的类型,也即是强制转换,比如:int a = int(1.23)(C++形式)或者int a = (int)1.23(C形式)其作用是将1.23转换为整形1。然而对于用户自定义的类类型,编译系统并不知道如何进行转换,所以需要定义专门的函数来告诉编译系统改如何转换,这就是转换构造函数类型转换函数

    一、转换构造函数

    转换构造函数(conversion constructor function) 的作用是将一个其他类型的数据转换成一个类的对象。
    当一个构造函数只有一个参数,而且该参数又不是本类的const引用时,这种构造函数称为转换构造函数。
    转换构造函数是对构造函数的重载。

    例如:

    Complex(double r)   
    {  
        real=r;  
        imag=0;  
    }  
    

    其作用是将double型的参数r转换成Complex类的对象,将r作为复数的实部,虚部为0。用户可以根据需要定义转换构造函数,在函数体中告诉编译系统怎样去进行转换。

    那么如何利用转换构造函数,进行自定义类型转换呢?看下面的例子:

    #include <iostream> 
    using namespace std;  
    
    class Complex
    {
    public:
    	Complex():real(0),imag(0)
    	{
    		cout << "Default ctor..." << endl;
    	}
    	Complex(double r, double i):real(r), imag(i)
    	{
    		cout << "Param ctor..." << endl;
    	}
    	Complex(double r):real(r),imag(0)
    	{
    		cout << "Convert ctor..." << endl;
    	} 
    
    	Complex(const Complex& c1)
    	{
    		cout << "Copy ctor..." << endl;
    		this->real = c1.real;
    		this->imag = c1.imag;
    	}
    	Complex& operator=(const Complex& c1)
    	{
    		cout << "Assign ctor..." << endl;
    		this->real = c1.real;
    		this->imag = c1.imag;
    		return *this;
    	}
    	Complex operator+(const Complex& c)
    	{
    		return Complex(this->real + c.real, this->imag + c.imag);
    	}
    
    
    
    	void Print()
    	{
    		cout << "real = " << real << "  imag = " << imag << endl;
    	}
    
    private:
    	double real, imag;
    };  
    
    int main()  
    {  
    	Complex c;
    	c = 1.2;
    	c.Print();
    
    	Complex c1(2.9, 4.2);
    	Complex c2 = c1 + 3.1;
    	c2.Print();
    
        return 0;  
    } 
    

      结果输出为:

    注:编译器是不会自动的添加转换构造函数的,因为它不知道怎么转换。

    二、类型转换函数

    用转换构造函数可以将一个指定类型的数据转换为类的对象。但是不能反过来将一个类的对象转换为一个其他类型的数据(例如将一个Complex类对象转换成double类型数据)。而类型转换函数就是专门用来解决这个问题的!
    类型转换函数的作用是将一个类的对象转换成另一类型的数据。
    如果已声明了一个Complex类,可以在Complex类中这样定义类型转换函数:

    operator double( )  
    {  
         return real;  
    } 
    

    类型转换函数的一般形式为:

    operator 类型名( )
    {
    	实现转换的语句
    }
    

    注意事项:
    1.在函数名前面不能指定函数类型,函数没有参数。
    2.其返回值的类型是由函数名中指定的类型名来确定的。
    3.类型转换函数只能作为成员函数,因为转换的主体是本类的对象,不能作为友元函数或普通函数。
    4.从函数形式可以看到,它与运算符重载函数相似,都是用关键字operator开头,只是被重载的是类型名。double类型经过重载后,除了原有的含义外,还获得新的含义(将一个Complex类对象转换为double类型数据,并指定了转换方法)。这样,编译系统不仅能识别原有的double型数据,而且还会把Complex类对象作为double型数据处理。

    看下面的代码:

    #include <iostream> 
    using namespace std;  
    
    class Complex
    {
    public:
    	Complex():real(0),imag(0)
    	{
    		cout << "Default ctor..." << endl;
    	}
    	Complex(double r, double i):real(r), imag(i)
    	{
    		cout << "Param ctor..." << endl;
    	}
    	//转换构造函数
    	Complex(double r):real(r),imag(0)
    	{
    		cout << "Convert ctor..." << endl;
    	} 
    	//重载+运算符
    	Complex operator+(const Complex& c)
    	{
    		return Complex(this->real + c.real, this->imag + c.imag);
    	}
    	//类型转换函数
    	operator double()
    	{
    		return real;
    	}
    
    private:
    	double real, imag;
    };  
    
    int main()  
    {  
        Complex c1(1.2, 2.3);  
        double d;  
        d = c1 + 1.1; // 调用类型转换函数将c1转换为double  
        cout<<d<<endl;  
    	
        return 0;  
    } 
    

      代码是不能通过编译的:

    error C2666: '+' : 2 overloads have similar conversions
    

     也就是说我们在这里定义了转换构造函数和转换函数,编译器不知道c1+1.1应该调用哪一个。到底是把c1转换成double,还是把1.1转换成对象,产生二义性。

    这里的解决办法就是,把转换构造函数去掉。但是如果把类型转换函数去掉,那么最后在把对象转换成double的时候还是通不过编译。

    总结:转换构造函数和类型转换函数就是在做自定义的类型同其它数据类型直接的方便的转换。(通过赋值符号=直接转换)

  • 相关阅读:
    Different AG groups have the exactly same group_id value if the group names are same and the ‘CLUSTER_TYPE = EXTERNAL/NONE’
    An example of polybase for Oracle
    use azure data studio to create external table for oracle
    Missing MSI and MSP files
    You may fail to backup log or restore log after TDE certification/key rotation.
    Password is required when adding a database to AG group if the database has a master key
    Use KTPASS instead of adden to configure mssql.keytab
    ardunio+舵机
    android webview 全屏100%显示图片
    glide 长方形图片显示圆角问题
  • 原文地址:https://www.cnblogs.com/stemon/p/4836611.html
Copyright © 2011-2022 走看看