【翁恺】30-运算符重载-类型转换

本文详细介绍了C++中内置类型与用户自定义类型的转换,包括隐式转换和显式转换。通过示例解释了如何使用单参数构造函数和转换操作符进行类型转换,并讨论了防止隐式转换的方法。强调了过度使用类型转换可能导致的问题,建议使用显式转换函数以提高代码可读性。
摘要由CSDN通过智能技术生成

value classes

  • appear to be primitive data types
  • passed to and returned from functions
  • have overloaded operators(often)
  • can be converted to and from other types
  • example: Complex, Data, String

user-defined type conversions

  • a conversion operator can be used to convert an object of one class into:
    • an object of another class
    • a built-in type
  • compilers perform implicit conversions using:
    • single-argument constructor
    • implicit type conversion operators

single argument constructors

class PathName{
    string name;
Public:
    //or could be muli-argument with defaults
    PathName(const string&);
    ~PathName();
};
...
string abc("abc");
PathName xyz(abc);//ok
xyz = abc;  //ok abc => PathName
class One{
public:
    One(){}
};
class Two{
public:
    Two(const One&){}
};
void f(Two){}
int main(){
    One one;
    f(one);    //wants Two.has a One
}

preventing implicit conversions

  • new keyword:explicit

    class PathName{
        string name;
    public:
        explicit PathName(const stirng&);  // 显式
        ~PathName();
    };
    ...
    string abc("abc");
    PathName xyz(abc);//ok!
    xyz = abc;//error
    class One{
    public:
        One(){}
    };
    class Two{
    public:
        explicit Two(const One&){}
    };
    void f(Two){}
    
    int main(){
        One one;
    //    f(one);//no auto conversion allowed
        f(Two(one));//ok--user performes conversion
    }

conversion operations

  • operator conversion
    • function will be called automatically
    • return type is same as function name
class Rational{
public:
    ...
    operator double() const;//Rational to double  this 变成 double
}
Rational::operator double() const{ // 没有返回类型
    return numerator_/(double)denominator_;
}
Rational r(1,3);
double d = 1.3*r;//r=> double

general form of conversion ops

  • X::operator T()
    • operator name is any type descriptor
    • no explicit arguments
    • no return type
    • complier will use it as type conversion from X=>T

c++ type conversions

  • built-in conversions

    • Primitivec
      • char => short => int => float => double int => long
    • Implicit (for any type T)
      • T => T&(赋值)        T& => T(初始化或赋值)     
      • T*  =>  void*           T[]  =>  T*       T*  => T[]         T => const T
  • user-defined T => C

    • if "C(T)" is a valid constructor call for c  c类有个构造函数
    • if operatorC() is defined for T   T类有operatorC()
    • BUT

      class Orange;//class declaration  前向声明
      
      class Apple{
      Public:
          operator Orange() const;//Convert Apple to Orange
      };
      class Orange{
      public:
          Orange(Apple);//Convert Apple to Orange
          explicit Orange(Apple);// 可以,但是尽量别用
      };
      void f(Orange){}
      
      int main(){
          Apple a;
          // f(a);//error:ambigunous conversion   编译器不知道用哪个
      }

do you want to use them?

  • in genaral, no!

    • cause lots of problems when functions are called unexpectedly
  • use explicit conversion functions.for example,in class Rational instead of the conversion operator ,declare a member function:

    double toDouble() const; // 更容易理解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理心炼丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值