转--C++ operator关键字(重载操做符)

operator是C++的关键字,它和运算符一块儿使用,表示一个运算符函数,理解时应将operator=总体上视为一个函数名。
 这是C++扩展运算符功能的方法,虽然样子古怪,但也能够理解:
一方面要使运算符的使用方法与其原来一致,另外一方面扩展其功能只能经过函数的方式(c++中,“功能”都是由函数实现的)。
 
1、为何使用操做符重载?
对于系统的全部操做符,通常状况下,只支持基本数据类型和标准库中提供的class,对于用户本身定义的class,若是想支持基本操做,好比比较大小,判断是否相等,等等,则须要用户本身来定义关于这个操做符的具体实现。好比,判断两我的是否同样大,咱们默认的规则是按照其年龄来比较,因此,在设计person 这个class的时候,咱们须要考虑操做符==,并且,根据刚才的分析,比较的依据应该是age。
那么为何叫重载呢?这是由于,在编译器实现的时候,已经为咱们提供了这个操做符的基本数据类型实现版本,可是如今他的操做数变成了用户定义的数据类型class,因此,须要用户本身来提供该参数版本的实现。
 
2、如何声明一个重载的操做符?
A:  操做符重载实现为类成员函数
重载的操做符在类体中被声明,声明方式如同普通成员函数同样,只不过他的名字包含关键字operator,以及紧跟其后的一个c++预约义的操做符。
能够用以下的方式来声明一个预约义的==操做符:
复制代码
 class person{
 private:
     int age;
 public:
     person(int a){
         this->age=a;
     }
     inline bool operator == (const person &ps) const;
 };
复制代码
实现方式以下:ios

 inline bool person::operator==(const person &ps) const
 {
     if (this->age==ps.age)
         return true;
     return false;
 }
调用方式以下:c++

复制代码
 #include
 using namespace std;
 int main()
 {
     person p1(10);
     person p2(20);
     if(p1==p2) cout<<”the age is equal!”< return 0;
 }
复制代码
这里,由于operator ==是class person的一个成员函数,因此对象p1,p2均可以调用该函数,上面的if语句中,至关于p1调用函数==,把p2做为该函数的一个参数传递给该函数,从而实现了两个对象的比较。函数

 

B:操做符重载实现为非类成员函数(全局函数)
对于全局重载操做符,表明左操做数的参数必须被显式指定。例如:
复制代码
 class person
 {
 public:
     int age;
 public:
 };
 
 bool operator==(person const &p1 ,person const & p2)
     //知足要求,作操做数的类型被显示指定
 {
     if(p1.age==p2.age)
         return true;
     return false;
 }
复制代码
调用方式以下:this

复制代码
 int main()
 {
     person rose;
     person jack;
     rose.age=18;
     jack.age=23;
     if(rose==jack)
         cout<<"ok"< return 0;
 }
复制代码
 spa

C:如何决定把一个操做符重载为类成员函数仍是全局名字空间的成员呢?
①若是一个重载操做符是类成员,那么只有当与他一块儿使用的左操做数是该类的对象时,该操做符才会被调用。若是该操做符的左操做数必须是其余的类型,则操做符必须被重载为全局名字空间的成员。
②C++要求赋值=,下标[],调用(), 和成员指向-> 操做符必须被定义为类成员操做符。任何把这些操做符定义为名字空间成员的定义都会被标记为编译时刻错误。
③若是有一个操做数是类类型如string类的情形那么对于对称操做符好比等于操做符最好定义为全局名字空间成员。
 
如下是C++ operator重载的例子设计

#include <iostream>
using namespace std;
class A
{
public:
    A(double _data = 0.0):data(_data){}
    A& operator = (const A& rhs)
    {
        data = rhs.data;
        return *this;
    }
     
    friend A operator + (const A& lhs,const A& rhs);
    friend A operator - (const A& lhs,const A& rhs);
    friend A operator * (const A& lhs,const A& rhs);
    friend A operator + (const A& lhs,double rhs);
    friend A operator + (double lhs,const A& rhs);
    friend A operator * (const A& lhs,double rhs);
    friend A operator * (double lhs,const A& rhs);
    friend A operator - (const A& lhs,double rhs);
    friend A operator - (double lhs,const A& rhs);
     
     
    friend ostream& operator << (ostream& fout,A& a);
//  A& operator += (const A& rhs);
//  A& operator -= (const A& rhs);
//  A& operator *= (const A& rhs); 
private:
    double data;
};
 
A operator + (const A& lhs,const A& rhs)
{
    A res(0);
    res.data = lhs.data + rhs.data;
    return res;
}
A operator - (const A& lhs,const A& rhs)
{
    A res(0);
    res.data = lhs.data - rhs.data;
    return res;
}
A operator * (const A& lhs,const A& rhs)
{
    A res(0);
    res.data = lhs.data * rhs.data;
    return res;
}
 A operator + (const A& lhs,double rhs)
 {
    A res(0);
    res.data = lhs.data + rhs;
    return res;
}
 
A operator + (double lhs,const A& rhs)
{
    A res(0);
    res.data = lhs + rhs.data;
    return res;
}
A operator * (const A& lhs,double rhs)
{
    A res(0);
    res.data = lhs.data * rhs;
    return res;
}
A operator * (double lhs,const A& rhs)
{
    A res(0);
    res.data = lhs * rhs.data;
    return res;
}
A operator - (const A& lhs,double rhs)
{
    A res(0);
    res.data = lhs.data - rhs;
    return res;
}
A operator - (double lhs,const A& rhs)
{
    A res(0);
    res.data = lhs - rhs.data;
    return res;
}
     
ostream& operator << (ostream& fout,A& a)
{
    fout << a.data ;
    return fout;
}
int main(int argc, char* argv[])
{
    A a(2.3);
    A b(1.2);
    A d(3.4);
    A c;
    c = a + b + d;
    c=a+b;
    c=a+1.0;
    c=a-b;
    c=a-1.0;
    c=a*b;
    c=a*1.0;
    cout << c << endl;
     
    c=1.0+2.0*a*a-3.0*a*b;
    cout << c << endl;
    return 0;
}对象

相关文章
相关标签/搜索