1348 字
7 分钟
3.4.5运算符重载
4.5 运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
4.5.1 加号运算符重载
作用:实现两个自定义数据类型相加的运算
#include<iostream>
class Person1 {public: Person1() {}; Person1(int a, int b) { this->m_A = a; this->m_B = b; } //成员函数实现 + 号运算符重载 Person1 operator+(const Person1& p) { Person1 temp; temp.m_A = this->m_A + p.m_A; temp.m_B = this->m_B + p.m_B; return temp; }
public: int m_A; int m_B;};
//全局函数实现 + 号运算符重载//Person1 operator+(const Person1& p1, const Person1& p2) {// Person1 temp(0, 0);// temp.m_A = p1.m_A + p2.m_A;// temp.m_B = p1.m_B + p2.m_B;// return temp;//}
//运算符重载 可以发生函数重载Person1 operator+(const Person1& p2, int val){ Person1 temp; temp.m_A = p2.m_A + val; temp.m_B = p2.m_B + val; return temp;}
void test1() {
Person1 p1(10, 10); Person1 p2(20, 20);
//成员函数方式 Person1 p3 = p2 + p1; //相当于 p2.operaor+(p1) std::cout << "mA:" << p3.m_A << " mB:" << p3.m_B << std::endl;
Person1 p4 = p3 + 10; //相当于 operator+(p3,10) std::cout << "mA:" << p4.m_A << " mB:" << p4.m_B << std::endl;
}
int main() {
test1();
return 0;}总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
总结2:不要滥用运算符重载
4.5.2 左移运算符重载
作用:可以输出自定义数据类型
#include<iostream>
class Person2 { friend std::ostream& operator<<(std::ostream& out, Person2& p);
public:
Person2(int a, int b) { this->m_A = a; this->m_B = b; }
//成员函数 实现不了 p << std::cout 不是我们想要的效果 //void operator<<(Person& p){ //}
private: int m_A; int m_B;};
//全局函数实现左移重载//ostream对象只能有一个std::ostream& operator<<(std::ostream& out, Person2& p) { out << "a:" << p.m_A << " b:" << p.m_B; return out;}
void test2() {
Person2 p1(10, 20);
std::cout << p1 << "hello world" << std::endl; //链式编程}
int main() {
test2();
return 0;}总结:重载左移运算符配合友元可以实现输出自定义数据类型
4.5.3 递增运算符重载
作用: 通过重载递增运算符,实现自己的整型数据
#include<iostream>
class MyInteger3 {
friend std::ostream& operator<<(std::ostream& out, MyInteger3 myint);
public: MyInteger3() { m_Num = 0; } //前置++ MyInteger3& operator++() { //先++ m_Num++; //再返回 return *this; }
//后置++ MyInteger3 operator++(int) { //先返回 MyInteger3 temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++; m_Num++; return temp; }
private: int m_Num;};
std::ostream& operator<<(std::ostream& out, MyInteger3 myint) { out << myint.m_Num; return out;}
//前置++ 先++ 再返回void test31() { MyInteger3 myInt; std::cout << ++myInt << std::endl; std::cout << myInt << std::endl;}
//后置++ 先返回 再++void test32() {
MyInteger3 myInt; std::cout << myInt++ << std::endl; std::cout << myInt << std::endl;}
int main() {
test31(); //test32();
return 0;}总结: 前置递增返回引用,后置递增返回值
4.5.4 赋值运算符重载
c++编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
示例 :
#include<iostream>
class Person4{public:
Person4(int age) { //将年龄数据开辟到堆区 m_Age = new int(age); }
//重载赋值运算符 Person4& operator=(Person4 &p) { if (m_Age != NULL) { delete m_Age; m_Age = NULL; } //编译器提供的代码是浅拷贝 //m_Age = p.m_Age;
//提供深拷贝 解决浅拷贝的问题 m_Age = new int(*p.m_Age);
//返回自身 return *this; }
~Person4() { if (m_Age != NULL) { delete m_Age; m_Age = NULL; } }
//年龄的指针 int *m_Age;
};
void test41(){ Person4 p1(18);
Person4 p2(20);
Person4 p3(30);
p3 = p2 = p1; //赋值操作
std::cout << "p1的年龄为:" << *p1.m_Age << std::endl;
std::cout << "p2的年龄为:" << *p2.m_Age << std::endl;
std::cout << "p3的年龄为:" << *p3.m_Age << std::endl;}
int main() {
test41();
//int a = 10; //int b = 20; //int c = 30;
//c = b = a; //std::cout << "a = " << a << std::endl; //std::cout << "b = " << b << std::endl; //std::cout << "c = " << c << std::endl;
return 0;}4.5.5 关系运算符重载
作用 :重载关系运算符,可以让两个自定义类型对象进行对比操作
示例 :
#include<iostream>#include<string>
class Person5{public: Person5(std::string name, int age) { this->m_Name = name; this->m_Age = age; };
bool operator==(Person5& p) { if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) { return true; } else { return false; } }
bool operator!=(Person5& p) { if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) { return false; } else { return true; } }
std::string m_Name; int m_Age;};
void test51(){ //int a = 0; //int b = 0;
Person5 a("孙悟空", 18); Person5 b("孙悟空", 18);
if (a == b) { std::cout << "a和b相等" << std::endl; } else { std::cout << "a和b不相等" << std::endl; }
if (a != b) { std::cout << "a和b不相等" << std::endl; } else { std::cout << "a和b相等" << std::endl; }}
int main() {
test51();
return 0;}4.5.6 函数调用运算符重载
- 函数调用运算符 () 也可以重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
示例 :
#include<iostream>#include<string>
class MyPrint{public: void operator()(std::string text) { std::cout << text << std::endl; }
};void test61(){ //重载的()操作符 也称为仿函数 MyPrint myFunc; myFunc("hello world");}
class MyAdd6{public: int operator()(int v1, int v2) { return v1 + v2; }};
void test62(){ MyAdd6 add; int ret = add(10, 10); std::cout << "ret = " << ret << std::endl;
//匿名对象调用 std::cout << "MyAdd6()(100,100) = " << MyAdd6()(100, 100) << std::endl;}
int main() {
test61(); test62();
return 0;}