运算符+重载
- 运算符重载含义
- 重载的方式
- 成员函数实现重载
- 全局函数实现重载
- 运算符重载也可以实现函数重载
- 运算符实现连续运算
运算符重载含义
- C++ 允许在同一作用域中的某个运算符指定多个定义,称为运算符重载。
- 通过运算符重载,可以使得已有的运算符具有其它的功能,适应于不同的操作数类型,经常运算符重载应用于自定义数据类型。
重载的方式
- 当成员函数和普通函数均可实现重载时,优先选用成员函数重载。
- 内置数据类型的表达式的运算符不能重载。比如3+2, 这个+不能变化其含义。
- 不要乱用重载,需要做到见名知意。
成员函数实现重载
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";// 用普通的成员函数实现自定义类型相加Horse horse_plus(Horse& h1) // 定义成员函数,实现Horse类型相加,返回Horse类型{Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}// 通过成员函数重载+,只是系统给自定义了名称operator+,使用+运算符和调用operator+及horse_plus函数是一样的Horse operator+ (Horse& h1){Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}};int main(){Horse h1;Horse h2;h1.age = 10;h2.age = 20;h1.color = "brown";h2.color = "white";cout << "horse_plus, age:" << h1.horse_plus(h2).age << endl; // 使用了成员函数实现加法cout << "horse_plus, color: " << h1.horse_plus(h2).color << endl; // 使用了成员函数实现加法cout << "+, age: " << (h1 + h2).age << endl; // + 运算符重载cout << "+, color: " << (h1 + h2).color << endl; // + 运算符重载cout << "operator+, age: " << h1.operator+(h2).age << endl; // + 运算符重载调用的本质其实是成员函数调用cout << "operator+, color : " << h1.operator+(h2).color << endl;// + 运算符重载调用的本质其实是成员函数调用system("pause");return 0;}
result:horse_plus, age:30horse_plus, color: brownwhite+, age: 30+, color: brownwhiteoperator+, age: 30operator+, color : brownwhite
全局函数实现重载
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";};// 通过全局函数重载+,系统给自定义了名称operator+,使用+运算符和调用operator+是一样的Horse operator+ (Horse& h1, Horse& h2) // 全局函数重载,参数要传两个{Horse result;result.age = h1.age + h2.age;result.color = h1.color + h2.color;return result;}int main(){Horse h1;Horse h2;h1.age = 10;h2.age = 20;h1.color = "brown";h2.color = "white";// h1 + h2相当于是operator+(h1, h2)cout << "+, age: " << (h1 + h2).age << endl; cout << "+, color: " << (h1 + h2).color << endl;cout << "operator+, age: " << operator+(h1, h2).age << endl;cout << "operator+, color : " << operator+(h1, h2).color << endl;system("pause");return 0;}
result:+, age: 30+, color: brownwhiteoperator+, age: 30operator+, color : brownwhite
运算符重载也可以实现函数重载
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";Horse horse_plus(Horse& h1){Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}// 通过成员函数重载+,只是系统给自定义了名称operator+,和调用operator+及horse_plus函数是一样的。Horse operator+ (Horse& h1){Horse result;result.age = this->age + h1.age;result.color = this->color + h1.color;return result;}// 运算符重载也可以实现函数重载Horse operator+ (int num){Horse result;result.age = this->age + num;return result;}};int main(){Horse h1;Horse h2;h1.age = 10;h2.age = 20;h1.color = "brown";h2.color = "white";cout << "horse_plus, age:" << h1.horse_plus(h2).age << endl;cout << "horse_plus, color: " << h1.horse_plus(h2).color << endl;cout << "+, age: " << (h1 + h2).age << endl; // 运算符重载实现的函数重载cout << "+, color: " << (h1 + h2).color << endl; // 运算符重载实现的函数重载cout << "operator+, age: " << h1.operator+(h2).age << endl; // 运算符重载调用的本质是函数调用cout << "operator+, color : " << h1.operator+(h2).color << endl;// 运算符重载调用的本质是函数调用cout << "operator+2, age: " << (h1 + 100).age << endl; // 运算符重载实现的函数重载system("pause");return 0;}
result:horse_plus, age:30horse_plus, color: brownwhite+, age: 30+, color: brownwhiteoperator+, age: 30operator+, color : brownwhiteoperator+2, age: 110
运算符实现连续运算
在int,float等类型的加法运算中,是可以实现连加的,那么自定义类型的对象的连加如何实现?
- 返回值必须是和运算符本身得到的结果的含义一致。如int+int,返回int,object+object,返回object。
- 返回值必须是对象的引用,这样连续操作时是对原对象进行操作,才能在原基础上练加。
- 当形参类型不同时,如果要实现操作数交换顺序,则需要进行运算符的函数重载。
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";Horse& operator+ (Horse& h1) // 运算符+重载,返回引用{Horse result;this->age = this->age + h1.age;this->color = this->color + h1.color;return *this; // this是对象本身的指针,*this是对象本身,返回值:自定义类型& 是对象本身的引用}};int main(){Horse h1;Horse h2;Horse h3;h1.age = 10;h2.age = 20;h3.age = 30;h1.color = "black";h2.color = "white";h3.color = "brown";cout << "+, age: " << (h1 + h2 + h3).age << endl;cout << "h1.color: " << h1.color << endl;cout << "+, color: " << (h1 + h2 + h3).color << endl;system("pause");return 0;
result:+, age: 60h1.color: blackwhitebrown+, color: blackwhitebrownwhitebrown
}
code:#include <iostream>using namespace std;class Horse{public:int age = 3;string color = "white";};Horse& operator+ (Horse& h1, Horse& h2){Horse result;h1.age = h1.age + h2.age;h1.color = h1.color + h2.color;return h1;}int main(){Horse h1;Horse h2;Horse h3;h1.age = 10;h2.age = 20;h3.age = 30;h1.color = "black";h2.color = "white";h3.color = "brown";cout << "+, age: " << (h1 + h2 + h3).age << endl;cout << "h1.color: " << h1.color << endl;cout << "+, color: " << (h1 + h2 + h3).color << endl;system("pause");return 0;}
result:+, age: 60h1.color: blackwhitebrown+, color: blackwhitebrownwhitebrown