第十四章 重载操作与转换(一)

    技术2024-06-27  65

    14.1 重载操作符的定义

    1)  ::   .*  .  ?: 4个符号不能重载

     

    2)  重载操作必须有一个类类型的操作数(强制重载操作符不能重新定义在已有的内置类型中)

     

    3)  优先级和结合性是固定的

     

    4)  重载后&&不再具有短路求值特性

    14.2 输入和输出操作符

    1)  必须作为非成员函数

     

    2)  输出

    ostream& operator<< (ostream& os,const ClassType &object)

    {

    os<<…//

    return os;

    }

     

    3)  输入

    必须处理错误和文件结束的可能性

    istream& operator<< (istream& in,const ClassType &object)

    {

    in>>…//

    if(in)

    else

    object = ClassType();//回复成空的

     

    return in;

    }

    14.3 算术操作符与关系操作符

    1)  一些例子

    A operator+(const A& a,const A& b);//注意返回值为了与内置操作保持一致,没有设置为引用,+可以通过+=来实现.

    inline bool operator == (const A&a,const A&b);//一般在实现!=等操作的时候,使用 ==

     

    2)  对于<操作符,由于很多关联容器和算法需要,最好定义为非成员函数.

    14.4 赋值操作符

    必须返回*this

    14.5 下标操作符

    由于可能对const 和非const对象都使用下标操作符,所以一般 声明如下

    int& operator[](const size_t);

    const int& operator[](const size_t) const ;

    14.6 成员访问操作符

    例子:

    A& operator*(){return *ptr->sp;}

    A* operator->(){return ptr->sp;}

    const A& operator*() const {return *ptr->sp;}

    const A* operator->() const {return ptr->sp;}

     

    重载箭头操作符必须参会指向类类型的指针,或定义了自己的指针操作符的类对象.

    14.7 自增操作符和自减操作符

    例子:

    A& operator++()//

    {

             ++member;

             return *this;

    }

    A& operator++(int)//,标识了一个int类型的0来区别前后++

    {

             A a(*this);

             ++*this;

             return a;

    }

    14.8 调用操作符和函数对象

    1)  例子:

    struct absInt{

             int operator () (int val){

                       return val<0?-val:val;

    }

    };

    int i=-42;

    absInt absObj;

    unsigned int ui = absObj(i);//类似于函数的使用方式,但是比函数方便;

     

    2)  用于标准库算法

    class GT_cls{

             public:

                       GT_cls(size_t val = 0):bound(val){}

                       bool operator()(const string&s)

                      {

                                return s.size()>=bound;

    }

             private:

                       std::string ::size_type bound;

    };

    count_if(words.begin(),words.end(),GT_cls(6));//返回长度小于6string的数量

     

    3)  标准库定义了一组对象类在 functional头文件中

    plus<T>                                                                                              +

    minus<T>                                                                                            -

    multiplies<T>                                                                                      *

    divides<T>                                                                                          /

    modulus<T>                                                                                       %

    negate<T>                                                                                          -

    equal_to<T>                                                                                       ==

    not_equal_to<T>                                                                                !=

    greater<T>                                                                                          >

    greater_equal<T>                                                                               >=

    less<T>                                                                                               <

    less_equal<T>                                                                                    <=

    logical_and<T>                                                                                   &&

    logical_or<T>                                                                                      ||

    logical_not<T>                                                                                    !

     

    例子:

    sort(svec.begin(),svec.end(),greater<string>());//降序排列string

     

    4)  函数对象对应的函数适配器

    bind2nd

    not1()

     

    例子:

    count_if(vec.begin(),vec.end(),bind2nd(less_equal<int>(),10));//小于10元素的个数的统计出来

    count_if(vec.begin(),vec.end(),not1(bind2nd(less_equal<int>(),10)));//不小于10

     

    最新回复(0)