运算符重载
运算符重载,本质上是函数重载。运算符重载不允许发明新的运算符。不能改变运算符操作对象的个数。
运算符被重载后,其优先级和结合性不会改变。
成员函数重载
成员函数原型的格式:函数类型 operator 运算符(参数表);
成员函数定义的格式:
函数类型 类名::operator 运算符(参数表)
{
函数体;
}
非成员函数重载(友元)
友元函数原型的格式:friend 函数类型 operator 运算符(参数表);
友元函数定义的格式:
函数类型 operator 运算符(参数表)
{
函数体;
}
#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
#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);
}
#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;
}
成员函数重载,1个参数。友元函数重载,2个参数。
我们实现了Add成员函数,但c1.Add(c2); 改变的是c1 本身;如果我们想实现加号表达式,c3 = c1 + c2; 那么可以实现operator+ 运算符重载,可以是成员函数形式,也可以是友元形式,如果两者共存的话成员函数优先。
加法运算符不能返回引用,因为不应该改变传入的参数。并且,引用不能返回局部变量。
String operator+(const String &s1, const String &s2)
{
//int len = strlen(s1.str_) + strlen(s2.str_) + 1;
//char* newstr = new char[len];
//memset(newstr, 0, len);
//strcpy(newstr, s1.str_);
//strcat(newstr, s2.str_);
//
//String tmp(newstr);
//delete newstr; 动态分配的内存,必须手动的释放掉
String str = s1;
str += s2;
return str;
}
加法运算符,必须用友员的方式实现。若使用成员函数的话,第一个函数必须是成员对象,隐含this指针。String s6 = "aaa" + s3;
第一个参数是字符串,编译报错。