zoukankan      html  css  js  c++  java
  • [019]转--C++ operator关键字(重载操作符)

    原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html

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

    实现方式如下:

    1 inline bool person::operator==(const person &ps) const
    2 {
    3     if (this->age==ps.age)
    4         return true;
    5     return false;
    6 }

    调用方式如下:

    1 #include
    2 using namespace std;
    3 int main()
    4 {
    5     person p1(10);
    6     person p2(20);
    7     if(p1==p2) cout<<”the age is equal!”< return 0;
    8 }

    这里,因为operator ==是class person的一个成员函数,所以对象p1,p2都可以调用该函数,上面的if语句中,相当于p1调用函数==,把p2作为该函数的一个参数传递给该函数,从而实现了两个对象的比较。

    B:操作符重载实现为非类成员函数(全局函数)
    对于全局重载操作符,代表左操作数的参数必须被显式指定。例如:
     1 class person
     2 {
     3 public:
     4     int age;
     5 public:
     6 };
     7 
     8 bool operator==(person const &p1 ,person const & p2)
     9     //满足要求,做操作数的类型被显示指定
    10 {
    11     if(p1.age==p2.age)
    12         return true;
    13     return false;
    14 }

    调用方式如下:

    1 int main()
    2 {
    3     person rose;
    4     person jack;
    5     rose.age=18;
    6     jack.age=23;
    7     if(rose==jack)
    8         cout<<"ok"< return 0;
    9 }
    C:如何决定把一个操作符重载为类成员函数还是全局名字空间的成员呢?
    ①如果一个重载操作符是类成员,那么只有当与他一起使用的左操作数是该类的对象时,该操作符才会被调用。如果该操作符的左操作数必须是其他的类型,则操作符必须被重载为全局名字空间的成员。
    ②C++要求赋值=,下标[],调用(), 和成员指向-> 操作符必须被定义为类成员操作符。任何把这些操作符定义为名字空间成员的定义都会被标记为编译时刻错误。
    ③如果有一个操作数是类类型如string类的情形那么对于对称操作符比如等于操作符最好定义为全局名字空间成员。
     
    以下是C++ operator重载的例子
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    #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;
    }


  • 相关阅读:
    厘摩(centimorgan,cM)到底是啥鬼
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第二周(Optimization algorithms) —— 2.Programming assignments:Optimization
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第二周(Optimization algorithms) —— 0.Learning Goals
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第一周(Practical aspects of Deep Learning) —— 4.Programming assignments:Gradient Checking
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第一周(Practical aspects of Deep Learning) —— 3.Programming assignments:Regularization
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第一周(Practical aspects of Deep Learning) —— 2.Programming assignments:Initialization
    课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第一周(Practical aspects of Deep Learning) —— 1.Practice Questions : Practical aspects of deep learning
    课程一(Neural Networks and Deep Learning)总结——2、Deep Neural Networks
    课程一(Neural Networks and Deep Learning)总结——1、Logistic Regression
    课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks) —— 3.Programming Assignments: Deep Neural Network
  • 原文地址:https://www.cnblogs.com/hustcser/p/4173758.html
Copyright © 2011-2022 走看看