运算符重载(操作符重载):可以为运算符增加一些新的功能
常见的运算符有:+ 、-、+=、==、!=、负号、++、—、<< 、>>等
class MyPoint {friend MyPoint add(MyPoint point1,MyPoint point2); private:int m_x;int m_y;public:MyPoint(int x,int y):m_x(x),m_y(y){};int getX(){return m_x;}int getY(){return m_y;}void print(){std::cout << "( " << m_x << " , " << m_y << " )" << std::endl;}};MyPoint add(MyPoint point1,MyPoint point2){ //能够访问MyPoint里面的所有成员变量return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);}
我们做两个点相加的时候,使用如下方式
int main(int argc, const char * argv[]) {MyPoint p1 = MyPoint(1,1);MyPoint p2 = MyPoint(2,2);MyPoint point = add(p1,p2);point.print();return 0;}
能否直接使用+号进行两个点相加呢?
这样是不行的,我们可以使用运算符重载的方式为+号添加这种功能。
class MyPoint {friend MyPoint operator+(MyPoint point1,MyPoint point2);private:int m_x;int m_y;public:MyPoint(int x,int y):m_x(x),m_y(y){};int getX(){return m_x;}int getY(){return m_y;}void print(){std::cout << "( " << m_x << " , " << m_y << " )" << std::endl;}};
MyPoint operator+(MyPoint point1,MyPoint point2){ //operator为运算符的标识return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);}
外界使用
int main(int argc, const char * argv[]) {MyPoint p1 = MyPoint(1,1);MyPoint p2 = MyPoint(2,2);MyPoint point = p1 + p2;//这句的本质其实调用了operator+这个函数// MyPoint point = operator+(p1, p2);point.print();return 0;}
下面两种写法是等价的
MyPoint point = p1 + p2;//这句的本质其实调用了operator+这个函数
上面的写法其实调用了如下函数,只是编译器允许我们直接使用+号运算符。
MyPoint point = operator+(p1, p2);
优化
对operator+进行优化
MyPoint operator+(MyPoint point1,MyPoint point2){ //operator为运算符的标识return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);}
参数如果是对象类型,会产生临时对象,调用拷贝构造函数,我们可以使用引用或指针作为参数
MyPoint operator+(MyPoint& point1,MyPoint& point2){ //operator为运算符的标识return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);}
参数类型最好是const,这样接收范围就大一些。const可以接受const类型的参数也可以接收非const类型的参数。
MyPoint operator+(const MyPoint& point1,const MyPoint& point2){ //operator为运算符的标识return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);}
外界调用
const MyPoint p1 = MyPoint(1,1);MyPoint p2 = MyPoint(1,1);MyPoint p3 = p1 + p2;
重载成员函数
我们把operator+变成成员函数 ```cpp
include
class MyPoint{
private: int m_x; int m_y; public:
MyPoint(int x,int y):m_x(x),m_y(y){};int getX(){return m_x;}int getY(){return m_y;}void print(){std::cout << "( " << m_x << " , " << m_y << " )" << std::endl;}MyPoint operator+(const MyPoint& point){ //operator为运算符的标识return MyPoint(m_x + point.m_x,m_y + point.m_y);}
};
外界使用
```cpp
int main(int argc, const char * argv[]) {
MyPoint p1 = MyPoint(1,1);
MyPoint p2 = MyPoint(1,1);
MyPoint p3 = p1 + p2;
return 0;
}
为了const类型的MyPoint也能调用,我们可以将operator+声明为const函数
MyPoint operator+(const MyPoint& point) const{ //operator为运算符的标识
return MyPoint(m_x + point.m_x,m_y + point.m_y);
}
运算符重载最好保留运算符原有的特性。比如
a + b是不能被赋值的。
我们的p1 + p2是可以被赋值的
MyPoint p1 = MyPoint(1,1);
MyPoint p2 = MyPoint(1,1);
p1 + p2 = MyPoint(1,1);
改造:如果返回的是一个常量,常量是不可以被赋值的
const MyPoint operator+(const MyPoint& point) const{ //operator为运算符的标识
return MyPoint(m_x + point.m_x,m_y + point.m_y);
}
仿函数(函数对象)
将一个对象当做一个函数一样来使用。对比普通函数,每次调用函数可以保存对象的一些状态。比如
#include <iostream>
using namespace std;
class MyPoint{
private:
int m_x;
int m_y;
public:
MyPoint(int x =0,int y=0):m_x(x),m_y(y){};
int operator()(int a,int b){
m_x = a;
m_y = b;
return a + b;
}
void print(){
cout << "( " << m_x << " , " << m_y << " )" << std::endl;
}
};
外界使用
int main(int argc, const char * argv[]) {
MyPoint point1;
int c = point1(1,1);//把对象当做函数来使用,本质还是调用point1.operator()(1, 1)
cout << c << endl;
return 0;
}
调用父类运算符重载函数
父类的成员变量通常是私有的,如何在运算符重载里面初始化父类的成员变量
#include <iostream>
using namespace std;
class Animal{
private:
int m_age;
public:
Animal(int age):m_age(age){};
Animal& operator=(const Animal &animal){
m_age = animal.m_age;
return *this;
}
};
class Cat : public Animal {
private:
int m_weight;
public:
Cat(int weight,int age):m_weight(weight),Animal(age){};
Cat& operator=(const Cat & cat){
this->m_weight = cat.m_weight;
Animal::operator=(cat); //调用父类的operator=初始化父类的成员变量
return *this;
}
};
外界使用
int main(int argc, const char * argv[]) {
Cat cat1(1,1);
Cat cat2(2,2);
Cat cat3 = cat2; //调用运算符=号
// cout << c << endl;
return 0;
}
练习
+=运算符
MyPoint& operator+=(const MyPoint& point) {
this->m_x = this->m_x + point.m_x;
this->m_y = this->m_y + point.m_y;
return *this;
}
==运算符
bool operator==(const MyPoint& point){
return (m_x == point.m_x)&& (m_y == point.m_y);
}
前置++
MyPoint& operator++(){ //前置++
m_x++;
m_y++;
return *this;
}
后置++
const MyPoint operator++(int){ //后置++,编译器为了区分是前置++还是后置++。使用int类型的参数作为区分,固定写法。
MyPoint old(m_x,m_y);
m_x++;
m_y++;
return old;
}
输出<<
需要使用全局函数,如果使用成员函数左边必须是类的对象(这里指Mypoint对象)调用。
#include <iostream>
using namespace std;
class MyPoint{
friend ostream& operator<<(ostream &cout, const MyPoint &point);
private:
int m_x;
int m_y;
public:
MyPoint(int x,int y):m_x(x),m_y(y){};
int getX(){
return m_x;
}
int getY(){
return m_y;
}
void print(){
cout << "( " << m_x << " , " << m_y << " )" << std::endl;
}
};
ostream& operator<<(ostream &cout, const MyPoint &point){
cout << "( " << point.m_x << "," <<point.m_y << " )";
return cout;
}
外界使用
MyPoint p1 = MyPoint(1,1);
MyPoint p2 = MyPoint(1,1);
cout << p1 << p2;
输入>>
#include <iostream>
using namespace std;
friend istream& operator>>(istream &cin,MyPoint &point);
private:
int m_x;
int m_y;
public:
MyPoint(int x,int y):m_x(x),m_y(y){};
int getX(){
return m_x;
}
int getY(){
return m_y;
}
void print(){
cout << "( " << m_x << " , " << m_y << " )" << std::endl;
}
};
istream& operator>>(istream &cin,MyPoint &point){
cin >> point.m_x;
cin >> point.m_y;
return cin;
}
外界使用
cin >> p1;
