十七、运算符重载(一) 成员函数重载、友元函数重载、运算符重载规则

一、运算符重载
        运算符重载允许把标准运算符(如+、—、*、/、<、>等)应用于自定义数据类型的对象,这样我们在用自己的数据类型进行运算时,写出的代码更直观易读。比如我们定义了了复数类Complex,重载了+-运算符之后我们就可以直接Complex c = a+b;在这一点上C++和Java不同,Java不允许运算符重载,但是我们经常在程序里看到String c = a+b;这样的写法,好像String重载了+运算符一样,实际上String中的+号会转换为StringBuffer的append方法操作,这是在java编译器做的处理,我们反编译之后就能看到String c = a+b;实际上被转换成了c = (new StringBuilder()).append(a).append(b).toString();
二、成员函数重载
成员函数原型声明的格式:

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

成员函数定义的格式:

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

  函数体;

  }

三、非成员函数重载
友元函数原型的格式:

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

友元函数定义的格式:

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

  函数体;

  }


四、运算符重载规则
虽然运算符重载非常有用,但是使用运算符重载的时候也有一些限制如下:
  • 运算符重载不允许发明新的运算符。
  • 不能改变运算符操作对象的个数。
  • 运算符被重载后,其优先级和结合性不会改变。
  • 不能重载的运算符:

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

注意:
注意:运算符重载仅仅只是语法上的方便,它是另一种函数调用的方式,它的本质上是函数重载另外,不要滥用重载、因为它只是语法上的方便,所以只有在涉及的代码更容易写、尤其是更易读时才有必要重载

例子:
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
//.h
class Complex
{
public:
	Complex(int real, int imag);
	Complex();
	~Complex();

	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 // _COMPLEX_H_
#include "Complex.h"
#include <iostream>
using namespace std;
//.cpp
Complex::Complex(int real, int imag) : real_(real), imag_(imag)
{
}

Complex::Complex()
{
}

Complex::~Complex()
{
}

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

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

	return Complex(r, i);
}

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

int main(void)
{
	Complex c1(3, 5);
	Complex c2(4, 6);
	Complex c3 = c1 + c2;		// 等价于c1.operator(c2) 或 opearator+(c1, c2);
	//Complex c3 = c1.operator +(c2);
	c1.Display();
	c2.Display();
	c3.Display();

	return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值