C++深度解析 操作符重载的概念 --- operator,成员函数的版本实现重载,全局函数的版本实现重载(29)
复数解决方案
//复数类
class Complex
{
public:
int a; //实部
int b; //虚部
};
int main()
{
//复数c1, c2, c3
Complex c1 = {1, 2};
Complex c2 = {3, 4};
Complex c3 = c1 + c2;
return 0;
}
示例程序:
#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;
}
//Add函数可以访问a和b
friend Complex Add(const Complex& p1, const Complex& p2);
};
Complex Add(const Complex& p1, const Complex& p2)
{
Complex ret;
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 = Add(c1, c2); //c1 + c2
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}
结果如下:
操作符重载
C++中的重载能够扩展操作符的功能
操作符的重载以函数的方式进行
本质:用特殊形式的函数扩展操作符的功能
通过operator关键字可以定义特殊的函数
operator的本质是通过函数 重载 操作符
语法:
//语法
Type operator Sign(const Type p1, const Type p2)
{
Type ret;
return ret;
}
//Sign为系统中预定义的操作符,如:+,-,*,/,等
示例程序:(全局操作符重载函数)
#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;
}
friend Complex operator + (const Complex& p1, const Complex& p2);
};
//告诉编译器,当两个对象相加的时候,就会调用下面的函数
Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
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; //等价于 operatoe + (c1, c2)
printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
return 0;
}
结果如下:
成员函数的操作符重载函数
可以将操作符重载函数定义为类的成员函数
- 比全局操作符重载函数少一个参数(左操作数)
- 不需要依赖友元就可以完成操作符重载
- 编译器优先在成员函数中寻找操作符重载函数
成员函数版本的操作符重载函数:
class Type
{
public:
Type operatoe Sign(const Type& p)
{
Type ret;
return ret;
}
};
示例程序一:
#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;
}
//只需要一个参数右操作数,左操作数是this参数
Complex operator + (const Complex& p)
{
Complex ret;
ret.a = this->a + p.a; //实部相加
ret.b = this->b + p.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;
}
结果如下:
分析:Complex c3 = c1 + c2 等价于 c1.operator+(c2),调用的参数是c2。左操作数是this参数,右操作数是c2。
示例程序二:(编译器优先在成员函数中寻找操作符重载函数)
#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;
}
//只需要一个参数右操作数,左操作数是this参数
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;
}
结果如下:
分析:成员函数确实可以用来操作符重载的功能,并且C++编译器优先调用成员函数的实现版本,只有当在成员函数里没有实现操作符重载函数时,编译器就会去找全局函数的实现版本。
小结
操作符重载是C++的强大特性之一。
操作符重载的本质是通过函数扩展操作符的功能。
operator关键字是实现操作符重载的关键。
操作符重载遵循相同的函数重载规则
全局函数和成员函数都可以实现对操作符重载