运算符重载

已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

运算符重载也可以发生函数重载

对于内置的数据类型的表达式的运算符是不可能改变的

不要滥用运算符重载

加号运算符重载

作用:实现两个自定义数据类型相加的运算

成员函数重载

#include <iostream>
using namespace std;
class Person
{
public:
//成员函数重载 +
Person operator+(Person& p)
{
Person temp;
temp.A = this->A + p.A;
temp.B = this->B + p.B;
return temp;
}
int A;
int B;
};
void test1()
{
Person p1;
p1.A = 10;
p1.B = 20;
Person p2;
p2.A = 30;
p2.B = 40;
Person p3;
//成员函数重载本质调用
//Person p3 = p1.operator+(p2);
p3 = p1 + p2;
cout << "p3.A = " << p3.A << endl;
cout << "p3.B = " << p3.B << endl;

}
int main()
{
test1();
}

全局函数重载

#include <iostream>
using namespace std;
class Person
{
public:
int A;
int B;
};
//全局函数重载 +
Person operator + (Person& p1, Person& p2)
{
Person temp;
temp.A = p1.A + p2.A;
temp.B = p1.B + p2.B;
return temp;
}
void test1()
{
Person p1;
p1.A = 10;
p1.B = 20;
Person p2;
p2.A = 30;
p2.B = 40;
Person p3;
//全局函数重载本质调用
//Person p3 = operator+ (p1, p2);
p3 = p1 + p2;
cout << "p3.A = " << p3.A << endl;
cout << "p3.B = " << p3.B << endl;

}
int main()
{
test1();
}

左移运算符重载

作用:可以输出自定义数据类型

#include <iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a, int b)
{
A = a;
B = b;
}
private:
int A;
int B;
};
//只能利用全局函数重载 <<
ostream& operator<<(ostream& cout, Person &p)
{
cout << "p1.A: " << p.A << endl;
cout << "p1.B: " << p.B << endl;
return cout;
}
void test1()
{
Person p1(10, 20);
cout << p1 << endl;
}
int main()
{
test1();
}

递增运算符重载

作用:通过递增运算符重载,实现自己的整型数据

#include <iostream>
using namespace std;
//自定义整型
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
MyInteger()
{
Num = 0;
}
//重载前置++运算符
//返回引用是为了一直对一个数据进行递增操作
MyInteger& operator++()
{
Num++;
//将自身做返回
return *this;
}
//重载后置++运算符
//int代表占位参数,可以用于区分前置++和后置++
//返回值不返回引用,避免非法操作
MyInteger operator++(int)
{
//先记录结果
MyInteger temp = *this;
//后递增
Num++;
//最后返回记录的结果
return temp;
}
private:
int Num;
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger& myint)
{
cout << myint.Num;
return cout;
}
void test1()
{
MyInteger myint;
cout << ++(++myint) << endl; //2
cout << myint << endl; //2
}
void test2()
{
MyInteger myint;
//需要分开重载左移运算符
cout << myint++ << endl; //0
cout << myint << endl; //1

}
int main()
{
//test1();
//test2();
}

赋值运算符重载

C++编译器至少给一个类添加4个函数

默认构造函数(无参,函数体为空)

默认析构函数(无参,函数体为空)

默认拷贝构造函数,对属性进行值拷贝

赋值运算符operator=,对属性进行值拷贝


如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
Age = new int(age);
}
~Person()
{
if (Age != NULL)
{
delete Age;
Age = NULL;
}
}
//重载 赋值运算符
Person& operator=(Person& p)
{
//编译器提供浅拷贝
//Age = p.Age;
//应该先判断是否有属性在堆区,如果有,先释放干净,然后再深拷贝
if (Age != NULL)
{
delete Age;
Age = NULL;
}
//让自身指针指向新的内存空间 (深拷贝)
Age = new int(*p.Age);
//返回对象本身
return *this;
}
int *Age;
};
void test1()
{
Person p1(18);
Person p2(20);
Person p3(22);
p3 = p2 = p1;
cout << "p1年龄为: " << *p1.Age << endl;
cout << "p2年龄为: " << *p2.Age << endl;
cout << "p3年龄为: " << *p3.Age << endl;
}
int main()
{
test1();
}

关系运算符重载

作用:可以让两个自定义类型对象进行对比操作

#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
Name = name;
Age = age;
}

//重载 ==
bool operator==(Person &p)
{
if (this->Name == p.Name && this->Age == p.Age)
{
return true;
}
return false;
}
string Name;
int Age;

};
void test1()
{
Person p1("zhangsan", 20);
Person p2("lisi", 20);
if (p1 == p2)
{
cout << "p1和p2相等" << endl;
}
else
{
cout << "p1和p2不相等" << endl;
}
}
int main()
{
test1();
}

函数调用运算符重载

函数调用运算符()也可以重载

由于重载后使用方式和调用函数非常相似,因此成为仿函数

仿函数没有固定写法,非常灵活


#include <iostream>
#include <string>
using namespace std;
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void test1()
{
MyPrint myprint;
myprint("hello");
}
int main()
{
test1();
}


#include <iostream>
using namespace std;
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test2()
{
MyAdd myadd;
int ret = myadd(100, 200);
cout << "ret = " << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 200) << endl;
}
int main()
{
test2();
}