目录
P121类和对象-C++运算符重载-加号运算符重载
P122类和对象-C++运算符重载-左移运算符重载
P123类和对象-C++运算符重载-递增运算符重载
P124类和对象-C++运算符重载-赋值运算符重载
P125类和对象-C++运算符重载-关系运算符重载
P126类和对象-C++运算符重载-函数调用运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
作用:实现两个自定义数据类型相加的运算
1、成员函数重载+号
示例:
using namespace std;
#include<iostream>
#include<string>
class Person {
public:
int m_A;
int m_B;
//1、通过自己写成员函数,实现两个对象相加属性后返回新对象
Person operator+(Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
};
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;//成员函数重载+号本质调用:Person p3=p1.operator+(p2);
cout << "p3.m_A = " << p3.m_A << endl;
cout << "p3.m_B = " << p3.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
结果:
2、全局函数重载+号
示例:
using namespace std;
#include<iostream>
#include<string>
class Person {
public:
int m_A;
int m_B;
};
Person operator+(Person& a, Person& b) {
Person temp;
temp.m_A = a.m_A + b.m_A;
temp.m_B = a.m_B + b.m_B;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;//全局函数重载+号本质调用:Person p3 = operator+(p1,p2)
cout << "p3.m_A = " << p3.m_A << endl;
cout << "p3.m_B = " << p3.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
结果:
3、运算符重载也可以发生函数重载
using namespace std;
#include<iostream>
#include<string>
class Person {
public:
int m_A;
int m_B;
//1、通过自己写成员函数,实现两个对象相加属性后返回新对象
/*Person operator+(Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}*/
};
Person operator+(Person& a, Person& b) {
Person temp;
temp.m_A = a.m_A + b.m_A;
temp.m_B = a.m_B + b.m_B;
return temp;
}
//函数重载版本
Person operator+(Person& p1, int num) {
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;//成员函数重载+号本质调用:Person p3=p1.operator+(p2);
//全局函数重载+号本质调用:Person p3 = operator+(p1,p2)
cout << "p3.m_A = " << p3.m_A << endl;
cout << "p3.m_B = " << p3.m_B << endl;
Person p4 = p1 + 20;
cout << "p4.m_A = " << p4.m_A << endl;
cout << "p4.m_B = " << p4.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
结果:
总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
总结2:不要滥用运算符重载
作用:可以输出自定义数据类型
using namespace std;
#include<iostream>
class Person {
public:
int m_A;
int m_B;
//不会利用成员函数重载左移运算符,因为无法实现cout在左侧
};
//只能利用全局函数重载左移运算符
void operator<<(ostream &cout, Person &p) //本质operator<<(cout,p)简化为cout<<p
{
cout << "m_A = " << p.m_A << " m_B = " << p.m_B << endl;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
cout << p1 ;
}
int main()
{
test01();
system("pause");
return 0;
}
作用: 通过重载递增运算符,实现自己的整型数据
示例:
using namespace std;
#include<iostream>
//自定义整型
class MyInteger {
friend ostream& operator<<(ostream& out, MyInteger myint);
public:
MyInteger() {
m_num = 0;
}
//重载前置++运算符 返回引用是为了一直对一个数据进行递增操作
MyInteger& operator++() {
//先++
m_num++;
//后返回
return *this;
}
//重载后置++运算符
//int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int) {
//先返回
MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
m_num++;
return temp;
}
private:
int m_num;
};
ostream& operator<<(ostream& out, MyInteger myint) {
out << myint.m_num;
return out;
}
//前置++ 先++ 再返回
void test01() {
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}
//后置++ 先返回 再++
void test02() {
MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
c++编译器至少给一个类添加4个函数
默认构造函数(无参,函数体为空)
默认析构函数(无参,函数体为空)
默认拷贝构造函数,对属性进行值拷贝
赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
示例:
class Person
{
public:
Person(int age)
{
//将年龄数据开辟到堆区
m_Age = new int(age);
}
//重载赋值运算符
Person& operator=(Person &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;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
//年龄的指针
int *m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
//int a = 10;
//int b = 20;
//int c = 30;
//c = b = a;
//cout << "a = " << a << endl;
//cout << "b = " << b << endl;
//cout << "c = " << c << endl;
system("pause");
return 0;
}
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
示例:
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
};
bool operator==(Person & p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
else
{
return false;
}
}
bool operator!=(Person & p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return false;
}
else
{
return true;
}
}
string m_Name;
int m_Age;
};
void test01()
{
//int a = 0;
//int b = 0;
Person a("孙悟空", 18);
Person b("孙悟空", 18);
if (a == b)
{
cout << "a和b相等" << endl;
}
else
{
cout << "a和b不相等" << endl;
}
if (a != b)
{
cout << "a和b不相等" << endl;
}
else
{
cout << "a和b相等" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
函数调用运算符 () 也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活
示例:
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01()
{
//重载的()操作符 也称为仿函数
MyPrint myFunc;
myFunc("hello world");
}
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
//匿名对象调用
cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}