【翁恺】28-运算符重载-原型

argument passing  参数传递

  • if it is read-only pass it in as a const reference(except built-ins)
  • make member functions const that don't change the class (boolean operators,+,-,etc)  不修改算子的const,修改的不加const
  • for global functions,if the left-hand side changes pass as a reference(assignment operators)

return values  返回值

  • select the return type depending on the expected meaning of the operator.for example
    • for operator + you need to generate a new object.return as a const object so the result cannot be modified as an value
    • logical operators should return bool(or int for older compilers)

operator  传进去的参数一定是对象,看运算符会不会修改算子,修改算子的传进去不能用  const(++,-- ,+=,-= 会修改算子),不修改算子的传进去的一定是const(+,-,*,/, =,...),this后面加const。

return value 看是否可以被修改,是否制造出一个新的对象,或者看是否可以做左值。例如operator+ 一定产生出一个新的对象,并且不能做左值,所以返回const对象。对于  operator=(赋值)修改了自己,能做左值,所以只能返回一个reference引用。index a[6] ,可以做左值,所以是reference引用。逻辑运算、关系运算(>=,<=,==,||,|,!)返回的是bool量。

对于重载一定对某个对象进行重载,所以传进去的参数一定是引用类型的。 

the prototypes of operators

  • +-*/%^&|~

    const T operatorX(const T& I,const T& r) const;
  • ! && || < <= == >= >

    bool operatorX(const T& I,const T& r) const;
  • []

    T& T::operator[](int index);

    operators ++ and --

  • how to distinguish postfix from prefix?

  • postfix forms take an int argument -- compiler will pass in () 0 as that int

    class Ineger{
    public:
        ...
        const Integer& operator++();//prefix++  ++a
        const Integer  operator++(int);//postfix++  a++   int不会真的起作用,参数不会被真的传进去,只是告诉编译器是哪种类型的 ++、--。
        const Integer& operator--();//prefix--
        const Integer  operator--(int);//prefix--
        ...
    };
    
    const Integer& Integer::operator++(){  //++a
        *this += 1; //increment  
        retrun *this;//fetch
    }
    //int argument not used so leave unnamed so won't get compiler warnings
    const Inreger Integer::operator++(int){   //a++
        Integer old(*this);//fetch  *this是一个对象,制造出一个新的对象old 调用拷贝构造
        ++(*this);//increment   ++a 那个函数会被调用,使用第一个函数定义第二个函数
        return old;//return  新的对象
    }

    using the overloaded ++ and --

    //decrement operators similar to increment
    Integer x(5);
    ++x;//calls x.operator++();
    x++;//calls x.operator++(0);
    --x;//calls x.operator--();
    x--;//calls x.operator--(0);
    • user-defined prefix is more efficient than postfix

relational operators 关系运算

  • implement != in terms of ==

  • implement >,>=,<= in terms of <

    class Integer{
        public:
        ...
            bool operator=={const Integer& rhs} const;
         	bool operator!={const Integer& rhs} const;
         	bool operator<{const Integer& rhs} const;
        	bool operator>{const Integer& rhs} const;
         	bool operator<={const Integer& rhs} const;
         	bool operator>={const Integer& rhs} const;
    }
    bool Ineger::operator==(const Ineger& rhs) const{
        return i == rhs.i;
    }
    //implement ihs != rhs in terms of !(lhs == rhs)
    bool Integer::operator!=(const Integer& rhs) const{
        return !(*this == rhs);// 调相等的函数来实现,为啥这么干?这里只定义了 == < 
    }
    bool Integer::operator<(const Integer& rhs) const{
        return !i < rhs.i;
    }
    bool Integer::operator>(const Integer& rhs) const{
        return rhs < *this;
    }
    bool Integer::operator<=(const Integer& rhs) const{
        return !(rhs < *this);
    }
    bool Integer::operator>=(const Integer& rhs) const{
        return !(*this < rhs);
    }

    其实只定义了等于和小于两个原函数--好处是方便重构

    都是内联函数,不担心性能问题,编译器会搞定。

operator[]  index

  • must be a member function

  • single argument

  • implies that the object it is being called for acts like an array,so it should return a reference

    Vector v(100);//create a vector of size 100
    v[10]=45;
    • note: if returned a pointer you would need to do

      *v[10] = 45;

 这么做是为了扩展。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

理心炼丹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值