C++运算符重载

1、operator加运算符表示运算符重载,运算符就是函数名。

2、容器类的类都需要重载=、==和<等运算符,用于排序或者复制元素。

3、不能重载“.”,反引用类成员指针".*",作用域解析符"::",三元运算符“?:",sizeof(),typeid(),类型转换符static_cast<>, dynamic_cast<>, const_cast<>, reinterpret_cast<>以及预处理符"#"。

4、++重载:

class Integer

{

public:

  Integer(int para){ m_data = para;}

  Integer operator++()  //前置版本,先自加,返回之后的值

  {

    m_data++;

    return *this;

  }

  

  Integer operator++(int) //后置版本,先自加,返回之前的值,int类型参数作为标志,为哑元参数。

  {

    Integer temp = *this;

    m_data++;

    return temp;

  }

private:

  int m_data;

};

 

posted @ 2017-11-26 17:45  一种微笑  Views(169)  Comments(0)    收藏  举报