欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

操作符重载(一)

程序员文章站 2022-05-18 20:59:58
[TOC] 1. 操作符重载的概念 操作符重载的本质 操作符重载的本质是用特殊形式的函数扩展操作符的功能 C++通过 关键字定义操作符重载函数 操作符重载遵循相同的函数重载规则 操作符重载的规则 在进行操作符重载时,必须遵循以下三条规则 不能改变原操作符的优先级 不能改变操作数的个数 不能改变操作符 ......

1. 操作符重载的概念

操作符重载的本质

操作符重载的本质是用特殊形式的函数扩展操作符的功能

  • c++通过operator关键字定义操作符重载函数
  • 操作符重载遵循相同的函数重载规则

操作符重载的规则

在进行操作符重载时,必须遵循以下三条规则

  • 不能改变原操作符的优先级
  • 不能改变操作数的个数
  • 不能改变操作符的原有语义

操作符重载的实现

全局函数和成员函数都可以实现对操作符的重载,重载为全局函数的语法规则为

/*
 * sign为预定义的操作符,如:+, -, *, /;
 * lp和rp分别为左操作数和右操作数.
*/
type operator sign (const type &lp, const type &rp)
{
}

如果重载为全局函数,则需要在友元的辅助下才能实现,因此,一般选择将操作符重载为类的成员函数

class type
{
public:
    type operator sign (const type &rp)
    {
    }
};
  • 比起全局操作符重载函数少一个参数(左操作数)
  • 不需要依赖友元就可以完成操作符重载
  • 编译器优先在成员函数中寻找操作符重载函数
#include <stdio.h>

class complex
{
    int a;
    int b;
public:
    complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }

    int geta()
    {
        return a;
    }

    int getb()
    {
        return b;
    }

    complex operator + (const complex &p)
    {
        complex ret;
        printf("complex operator + (const complex& p)\n");
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;

        return ret;
    }

    friend complex operator + (const complex &p1, const complex &p2);
};

complex operator + (const complex &p1, const complex &p2)
{
    complex ret;
    printf("complex operator + (const complex& p1, const complex& p2)\n");
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;

    return ret;
}

int main()
{
    complex c1(1, 2);
    complex c2(3, 4);
    complex c3 = c1 + c2; // c1.operator + (c2)

    printf("c3.a = %d, c3.b = %d\n", c3.geta(), c3.getb());

    return 0;
}

操作符重载(一)

2. 复数类的实现

下面,我们通过一个复数类,来分别实现算术运算操作符、比较操作符和赋值操作符的重载,以下是头文件和源文件的非重载部分代码。

complex.h

#ifndef _complex_h_
#define _complex_h_

class complex
{
    double a;
    double b;
public:
    complex(double a = 0, double b = 0);
    double geta();
    double getb();
    double getmodulus();

    complex operator + (const complex &c);
    complex operator - (const complex &c);
    complex operator * (const complex &c);
    complex operator / (const complex &c);

    bool operator == (const complex &c);
    bool operator != (const complex &c);

    complex &operator = (const complex &c);
};

#endif

complex.cpp

#include "complex.h"
#include <cmath>

complex::complex(double a, double b)
{
    this->a = a;
    this->b = b;
}

double complex::geta()
{
    return a;
}

double complex::getb()
{
    return b;
}

double complex::getmodulus()
{
    return sqrt(a * a + b * b);
}

运算操作符重载

complex.cpp

complex complex::operator + (const complex &c)
{
    complex ret;

    ret.a = a + c.a;
    ret.b = b + c.b;

    return ret;
}

complex complex::operator - (const complex &c)
{
    complex ret;

    ret.a = a - c.a;
    ret.b = b - c.b;

    return ret;
}

complex complex::operator * (const complex &c)
{
    complex ret;

    ret.a = a * c.a - b * c.b;
    ret.b = a * c.b + b * c.a;

    return ret;
}

complex complex::operator / (const complex &c)
{
    complex ret;
    double cm = c.a * c.a + c.b * c.b;

    ret.a = (a * c.a + b * c.b) / cm;
    ret.b = (b * c.a - a * c.b) / cm;

    return ret;
}

比较操作符重载

complex.cpp

bool complex::operator == (const complex &c)
{
    return (a == c.a) && (b == c.b);
}

bool complex::operator != (const complex &c)
{
    return !(*this == c);
}

赋值操作符重载

complex.cpp

complex &complex::operator = (const complex &c)
{
    if (this != &c)
    {
        a = c.a;
        b = c.b;
    }

    return *this;
}

注意,赋值操作符有几点特殊之处:

  • c++规定赋值操作符只能重载为成员函数
  • 赋值操作符重载函数返回类型为引用,目的是为了实现连等,如a = b = c

复数类测试

main.cpp

#include "complex.h"
#include <cstdio>

int main()
{
    complex c1(1, 2);
    complex c2(3, 6);
    complex c3 = c2 - c1;
    complex c4 = c1 * c3;
    complex c5 = c2 / c1;

    printf("c3.a = %f, c3.b = %f\n", c3.geta(), c3.getb());
    printf("c4.a = %f, c4.b = %f\n", c4.geta(), c4.getb());
    printf("c5.a = %f, c5.b = %f\n", c5.geta(), c5.getb());

    complex c6(2, 4);

    printf("c3 == c6 : %d\n", c3 == c6);
    printf("c3 != c4 : %d\n", c3 != c4);

    (c3 = c2) = c1;

    printf("c1.a = %f, c1.b = %f\n", c1.geta(), c1.getb());
    printf("c2.a = %f, c2.b = %f\n", c2.geta(), c2.getb());
    printf("c3.a = %f, c3.b = %f\n", c3.geta(), c3.getb());

    return 0;
}

操作符重载(一)

3. 赋值操作符重载和拷贝构造函数

赋值操作符重载与深拷贝

赋值操作符重载和拷贝构造函数具有相同的作用和意义,那么,c++什么时候调用赋值操作符重载函数?什么时候调用拷贝构造函数?

classname c1;
classname c2 = c1; //调用拷贝构造函数
classname c3(c1);  //调用拷贝构造函数

classname c4;
c4 = c1;           //调用赋值操作符重载函数

和拷贝构造函数类似

  • 编译器为每个类默认重载了赋值操作符
  • 默认的赋值操作符重载函数仅完成浅拷贝
  • 当需要进行深拷贝时,必须重载赋值操作符

作为一般性原则,重载赋值操作符,必然需要实现深拷贝!!!

#include <iostream>
#include <string>

using namespace std;

class test
{
    int *m_pointer;
public:
    test()
    {
        m_pointer = null;
    }

    test(int i)
    {
        m_pointer = new int(i);
    }

    /*拷贝构造函数实现深拷贝*/
    test(const test &obj)
    {
        m_pointer = new int(*obj.m_pointer);
    }

    /*重载赋值操作符实现深拷贝*/
    test &operator = (const test &obj)
    {
        if (this != &obj)
        {
            delete m_pointer;
            m_pointer = new int(*obj.m_pointer);
        }

        return *this;
    }

    void print()
    {
        cout << "m_pointer = " << hex << m_pointer << endl;
    }

    ~test()
    {
        delete m_pointer;
    }
};

int main()
{
    test t1 = 1;
    test t2;

    t2 = t1;

    t1.print();
    t2.print();

    return 0;
}

操作符重载(一)

数组类intarray改进

intarray.h

#ifndef _intarray_h_
#define _intarray_h_

class intarray
{
public:
    intarray &operator = (const intarray &obj); //add
};

#endif

intarray.cpp

intarray &intarray::operator = (const intarray &obj)
{
    if (this != &obj)
    {
        int *pointer = new int[obj.m_length];

        if (pointer != null)
        {
            for (int i = 0; i < obj.m_length; i++)
            {
                pointer[i] = obj.m_pointer[i];
            }

            m_length = obj.m_length;
            delete m_pointer;
            m_pointer = pointer;
        }
    }

    return *this;
}