zoukankan      html  css  js  c++  java
  • 以成员函数方式重载、以友元函数方式重载

    一、运算符重载

    运算符重载允许把标准运算符(如+、-、*、/、<、>等)应用于自定义数据类型的对象
    直观自然,可以提高程序的可读性
    体现了C++的可扩充性
    运算符重载仅仅只是语法上的方便,它是另一种函数调用的方式
    运算符重载,本质上是函数重载
    不要滥用重载、因为它只是语法上的方便,所以只有在涉及的代码更容易写、尤其是更易读时才有必要重载

    二、成员函数重载

    成员函数原型的格式:

    函数类型 operator 运算符(参数表);

    成员函数定义的格式:

    函数类型 类名::operator 运算符(参数表)

    {

    函数体;

    }


    三、非成员函数重载

    友元函数原型的格式:

    friend 函数类型 operator 运算符(参数表);

    友元函数定义的格式:

    函数类型 类名::operator 运算符(参数表)

    {

    函数体;

    }


    四、运算符重载的原则

    运算符重载不允许发明新的运算符。
    不能改变运算符操作对象的个数。
    运算符被重载后,其优先级和结合性不会改变。
    不能重载的运算符:

    作用域解析运算符 ::

    条件运算符 ? :

    直接成员访问运算符 .

    类成员指针引用的运算符 .*

    sizeof运算符 sizeof

    注:.*是C++的类成员函数指针调用运算符,是用来调用一个类函数指针的。
    举例:
    假设有一个ClassA类拥有一个成员函数void ClassA::func(int i),则可以这么用:
    void (ClassA::*fp)(int i) // 定义一个类函数指针。
    ClassA obj; 
    fp = &ClassA::func; // 给这个类函数指针赋值
    (obj.*fp)(5); // 这么用,相当于调用obj.func(5);

    一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。
    以下一些双目运算符不能重载为类的友元函数:=、()、[]、->。
    类型转换运算符只能以成员函数方式重载(见这里
    流运算符只能以友元的方式重载(见这里

     C++ Code 
    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
     
    #ifndef _COMPLEX_H_
    #define _COMPLEX_H_


    class Complex
    {
    public:
        Complex(int real, int imag);
        Complex(void);
        ~Complex(void);

        Complex &Add(const Complex &other);

        void Display() const;

        Complex operator+(const Complex &other);

        friend Complex operator+(const Complex &c1, const Complex &c2);

    private:
        int real_;
        int imag_;
    };

    #endif
     C++ Code 
    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
     

    #include "Complex.h"
    #include<iostream>
    using namespace std;

    Complex::Complex(int real, int imag): imag_(imag), real_(real)
    {

    }
    Complex::Complex(void)
    {
    }

    Complex::~Complex(void)
    {
    }

    Complex &Complex::Add(const Complex &other)
    {
        real_ += other.real_;
        imag_ += other.imag_;
        return *this;
    }


    void Complex::Display() const
    {
        cout << real_ << "+" << imag_ << "i" << endl;
    }


    Complex Complex::operator+(const Complex &other)
    {
        int r = real_ + other.real_;
        int i = imag_ + other.imag_;

        return Complex(r, i);
    }

    Complex operator+(const Complex &c1, const Complex &c2)
    {
        int r = c1.real_ + c2.real_;
        int i = c1.imag_ + c2.imag_;

        return Complex(r, i);
    }
     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    #include "Complex.h"

    int main(void)
    {
        Complex c1(3, 5);
        Complex c2(4, 6);

        c1.Add(c2);
        c1.Display();

        Complex c3 = c1 + c2; // 等价于c1.opertor+(c2); 或 operator+(c1, c2);

        c3.Display();

        return 0;
    }

    我们实现了Add成员函数,但c1.Add(c2); 改变的是c1 本身;如果我们想实现加号表达式,c3 = c1 + c2; 那么可以实现operator+ 运算符重载,可以是成员函数形式,也可以是友元形式,如果两者共存的话成员函数优先。


    参考:

    C++ primer 第四版
    Effective C++ 3rd
    C++编程规范

  • 相关阅读:
    FreeRTOS相关转载-(朱工的专栏)
    任务相关的API函数-uxTaskGetSystemState
    STM32用FreeRTOS时任务优先级和中断优先级说明
    STM32标准外设库中USE_STDPERIPH_DRIVER, STM32F10X_MD的含义
    C语言变量和函数命名规范
    Java学习笔记-命令模式
    leetcode-Search in Rotated Sorted Array -java
    Java学习笔记-单件模式
    Java学习笔记-问问题?-->静态方法
    TCP滑动控制
  • 原文地址:https://www.cnblogs.com/alantu2018/p/8471053.html
Copyright © 2011-2022 走看看