• 22、C++ Primer 4th 笔记,到类类型与从类类型的转换


    1转换分为到类类型与从类类型的转换两种。到类类型的转换:通过转换构造函数;从类类型的转换:转换操作符。

    2、常用16个操作符:5个算术操作符(+-*/%)及其对应的复合赋值操作符,4 个关系操作符(<<=>>=),以及相等操作符(==!=)。

    示例

    class SmallInt
    {
    public:
    	SmallInt(int i = 0):val(i)
    	{
    		//...
    	}
    	operator int() const {return val;} //转换操作符函数,由SmallInt类类型转换成int型。
    private:
    	std::size_t val;
    };
    
    SmallInt si;
    double dval;
    si >= dval // si converted to int and then convert to double
    if (si) // si converted to int and then convert to bool
    

    通用形式:operator type();

        type表示内置类型名,类类型名或由类型别名所定义的名字。对任何可作为函数返回类型的类型(除了void之外),都可以定义转换函数。一般而言,不允许转换为数组或函数类型,转换为指针类型(数据或函数指针)及引用是可以的。

    转换操作符函数必须及必须是成员函数,不能指定返回类型,形参表必须为空。

        类类型转换不能传递(两个都是自定义的转换,而非内置类型的默认转换),如A->B,B->C,但是我们不能把需要C类型参数的地方传递A类型的参数。

    示例

    class SmallInt
    {
    public:
    	SmallInt(int i = 0):val(i)  //(转换)构造函数
    	{
    		//...
    	}
    	operator int() const {return val;} //转换操作符函数,由SmallInt类类型转换成int型。
    private:
    	std::size_t val;
    };
    class Integral {
    public:
    	Integral(int i = 0): val(i) { }
    	operator SmallInt() const { return val % 256; }
    private:
    	std::size_t val;
    };
    int calc(int);
    Integral intVal;
    SmallInt si(intVal); // ok: convert intVal to SmallInt and copy to si
    int i = calc(si); // ok: convert si to int and call calc
    int j = calc(intVal); // error: no conversion to int from Integral
    

    3、完全匹配转换比需要标准转换的其他转换更好。

    4、转换的二义性

    示例二义性

    #include "iostream"
    #include "vector"
    #include "algorithm"
    #include "string"
    #include "functional"
    using namespace std;
    
    class Integral;
    class SmallInt
    {
    public:
    	SmallInt(Integral){}
        //...
    };
    class Integral
    {
    public:
    	operator SmallInt() const{}
    	//...
    };
    
    void compute(SmallInt);
    
    int main()
    {
    Integral int_val;
    compute(int_val); //error:ambiguous
    }
    

    避免二义性最好的方法是避免编写互相提供隐式转换的成对的类。保证最多只有一种途经将一个类型转换成另一个类型。

    可以通过显式强制消除二义性。

    示例

    #include "iostream"
    #include "vector"
    #include "algorithm"
    #include "string"
    #include "functional"
    using namespace std;
    
    class SmallInt {
    public:
    	// Conversions to int or double from SmallInt
    	// Usually it is unwise to define conversions to multiple arithmetic types
    	operator int() const { return val; }
    	operator double() const { return val; }
    	// ...
    private:
    	std::size_t val;
    };
    void compute(int);
    void compute(double);
    void compute(long double);
    
    int main()
    {
    	SmallInt si;
    	//compute(si); // error: ambiguous
    	//compute(static_cast<int>(si)); //ok, call compute(int)
    }
    

        有调用重载函数时,需要使用构造函数或强制类型转换来转换实参是拙劣设计的表现。

    5、操作符的重载确定,三步:

    1)选择候选函数。

    2)选择可行函数,包括识别每个实参的潜在转换序列。

    3)选择最佳匹配的函数。

    6、几个经验

    1)不要定义相互转换的类,即如果类 Foo 具有接受类 Bar 的对象的构造函数,不要再为类 Bar 定义到类型 Foo 的转换操作符。

    2)避免到内置算术类型的转换。具体而言,如果定义了到算术类型的转换,则

        o不要定义接受算术类型的操作符的重载版本。如果用户需要使用这些操作符,转换操作符将转换你所定义的类型的对象,然后可以使用内置操作符。

        o不要定义转换到一个以上算术类型的转换。让标准转换提供到其他算术类型的转换。

        最简单的规则是:对于那些“明显正确”的,应避免定义转换函数并限制非显式构造函数。

  • 相关阅读:
    DeepLearning之路(三)MLP
    DeepLearning之路(二)SoftMax回归
    DeepLearning之路(一)逻辑回归
    自然语言处理工具
    一个 11 行 Python 代码实现的神经网络
    对联广告
    Java多线程
    QT数据库操作
    QT笔记
    C++基础入门
  • 原文地址:https://www.cnblogs.com/mydomain/p/1997017.html
Copyright © 2020-2023  润新知