网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
Person p3 = p2 + p1; //相当于 Person p3 = operator+(p1,p2);
//运算符重载
Person p4 = p3 + 10; //相当于 operator+(p3,10)
cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;
}
int main() {
test();
system("pause");
return 0;
}
> 总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
> 总结2:不要滥用运算符重载(声明与实现应该一致)
#### []( )4.5.2 左移运算符重载
作用:可以输出自定义数据类型
class Person {
friend ostream& operator<<(ostream& out, Person& p);//友元函数
public:
Person(int a, int b)//构造函数
{
this->m_A = a;
this->m_B = b;
}
//成员函数本质 p.operator<<(cout) 简化版本 p << cout 不是我们想要的效果
//无法实现cout在左侧
//void operator<<(cout){
//}
private:
int m_A;
int m_B;
};
//全局函数实现左移重载
//ostream对象只能有一个,所以必须用引用(指针常量)传递
//返回值为 ostream& 类型,可以实现链式编程
ostream& operator<<(ostream& out, Person& p) {
out << "a:" << p.m_A << " b:" << p.m_B;
return out;
}
void test() {
Person p1(10, 20);
cout << p1 << "hello world" << endl; //链式编程
}
int main() {
test();
system("pause");
return 0;
}
> 总结:重载左移运算符配合友元可以实现输出自定义数据类型
#### []( )4.5.3 递增运算符重载
作用: 通过重载递增运算符,实现自己的整型数据
class MyInteger {//自定义的整型类
friend ostream& operator<<(ostream& out, MyInteger myint);//友元函数
public:
MyInteger() {
m_Num = 0;
}
//前置++ 前置递增返回引用
MyInteger& operator++() {
//先++
m_Num++;
//再返回
return *this;
}
//后置++ 后置递增返回值
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() {
test01();
//test02();
system("pause");
return 0;
}
> 总结: 前置递增返回引用,后置递增返回值
#### []( )4.5.4 赋值运算符重载
c++编译器至少给一个类添加4个函数
1. 默认构造函数(无参,函数体为空)
2. 默认析构函数(无参,函数体为空)
3. 默认拷贝构造函数,对属性进行值拷贝
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;
}
#### []( )4.5.5 关系运算符重载
\*\*作用:\*\*重载关系运算符,可以让两个自定义类型对象进行对比操作
**示例:**
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{
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
}
else{
return false;
}
}
//重载关系运算符!=
bool operator!=(Person & p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){
return false;
}
else{
[外链图片转存中…(img-lUeEQTKi-1715547424410)]
[外链图片转存中…(img-v5P6WW9a-1715547424410)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!