20240418,运算载重符(根本写不对后置递增)

被整的有点EMO了,还好还好,我是懂怎么怎么让(表现出)相同观点的人说出我想听的话的

目录

4.1 加号

0.1 通过成员函数重载

0.2 通过全局函数重载

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

4.2 左移

4.3 递增~递减~

01. 两个成员变量,前置OK,后置BUG——为什么!

02. 一个成员变量,后置BUG ——到底为什么!

0.3 照着视频打的,错误——所以为什么我的错了啊啊啊啊啊,我检查不出来

 0.4 复制的,正确——到底为什么!!!!

00001 我把照着视频打的函数部分,粘贴到正确的,能运行,但是有提示“没有与这些操作数匹配的<<运算符

00002 我人真的麻了,去你MUA的

0.5 递减(前置) 

0.6 递减(前置)——呵呵呵,一摸一样的BUG

0.7 正确的,俺小弟的代码……此时我还是不知道我的代码有什么BUG……专业的就是不一样……代码都不偷懒的

0.8 GPT改了能跑的:Q1:递减之前返回,函数结束,没有递减 Q2:重载函数应常量引用

4.4 赋值

4.5 关系,>,<,!=,==

4.6 函数调用

四,运算载重符

对已有的运算符进行定义,赋予其另一种功能,以适应不同的数据类型
本质是函数调用的一种默认简化表达,主要是针对类的?

4.1 加号
0.1 通过成员函数重载
#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
	Person (){}//乐.有参有拷贝,拷贝都没有
	Person(int a,int b)
	{
		m_a = a;
		m_b = b;
	}
	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(30, 3);
	Person p2(32, 45);
	Person p3 = p2.operator+ (p1);//成员函数重载的本质调用
	cout << "p3的m_a值为" << p3.m_a << endl;
	cout << "p3的m_b值为" << p3.m_b << endl;
	p3 = 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;
}
0.2 通过全局函数重载
#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
	Person (){}//乐.有参有拷贝,拷贝都没有
	Person(int a,int b)
	{
		m_a = a;
		m_b = b;
	}
};
Person operator+ (Person& p1,Person &p2)
{
	Person temp;
	temp.m_a = p1.m_a + p2.m_a;
	temp.m_b = p1.m_b + p2.m_b;
	return temp;
}
void test01()
{
	Person p1(30, 3);
	Person p2(32, 45);
	Person p3 = operator+ (p1,p2);//全局函数重载的本质调用
	cout << "p3的m_a值为" << p3.m_a << endl;
	cout << "p3的m_b值为" << p3.m_b << endl;
	p3 = 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;
}
0.3 运算符重载也可以发生函数重载
#include<iostream>
using namespace std;
class Person
{
public:
	int m_a;
	int m_b;
	Person (){}//乐.有参有拷贝,拷贝都没有
	Person(int a,int b)
	{
		m_a = a;
		m_b = b;
	}
};
Person operator+ (Person& p1,int a)
{
	Person temp;
	temp.m_a = p1.m_a + a;
	temp.m_b = p1.m_b + a;
	return temp;
}
void test01()
{
	Person p1(30, 3);
	Person p3 = operator+ (p1,10);//全局函数重载的本质调用
	cout << "p3的m_a值为" << p3.m_a << endl;
	cout << "p3的m_b值为" << p3.m_b << endl;
	p3 = p1 + 10;//简化形式
	cout << "p3的m_a值为" << p3.m_a << endl;
	cout << "p3的m_b值为" << p3.m_b << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

对于内置的数据类型的表达式的运算符式不可能改变的,不要滥用重载运算符 

4.2 左移

友元,OSTREAM数据类型,只全局函数重载

#include<iostream>
using namespace std;
class Person
{
	friend ostream& operator<< (ostream& out, Person& p);//友元一下
	friend void test01();//友元两下
private:
	int m_a;
	int m_b;
	Person (){}//乐.有参有拷贝,拷贝都没有
	Person(int a,int b)//或者把这个成员函数PUBLIC
	{
		m_a = a;
		m_b = b;
	}
	//void operator<<(cout)
	//如果返回cout,使用时p.operator(cout),简化版本p<<cout
	//不会利用成员函数重载<<
};
//只能全局函数重载
//返回一个输出流类型的变量
ostream& operator<< (ostream &out,Person &p)//本质operator<<(cout,p),简化cout<<p
{                 //cout输出流对象,全局只有一个,只能传指针/引用?
	              //引用本身式起别名
	cout << "m_a=" << p.m_a << endl;
	cout << "m_b=" << p.m_b << endl;
	return out;
}
void test01()
{
	Person p1(30, 3);
	cout << p1 <<"运算完成"<< endl;//当函数返回VOID的时候,不符合链式调用,<<endl前面不是cout
}
int main()
{
	test01();
	system("pause");
	return 0;
}
4.3 递增~递减~
01. 两个成员变量,前置OK,后置BUG——为什么!

我在犯蠢+放屁,别管——后置++输出报错:没有找到接受“Person”类型的右操作数的运算符(或没有可接受的转换);猜测是返回了Person的值?而不是一个PERSON类型的变量,但是Person有两个值,不满足链式的书写结构,就报错了??????啊啊啊

#include<iostream>
using namespace std;
class Person
{
	friend ostream& operator<< (ostream& out, Person& p);
public:
	Person()
	{
		m_num = 0;
		m_num1 = 100;
	}
	//重载前置++
	//返回引用保证一直对一个对象运算
	Person& operator++ ()//operator++ 额外参数必须是INT类型
	{
		m_num++;//前置后置,都只返回THIS,结果没差
		m_num1++;
		return *this;//返回自身
	}
	Person operator++ (int)//int 代表占位参数,可以用于区分前置和后置递增
	{
		//先返回结果,再递增
		Person temp= *this;
		m_num++;
		m_num1++;
		//最后返回递增结果
		return temp;//temp是局部变量,函数结束就被释放,这里是返回值
	}
private:
	int m_num;
	int m_num1;
};

ostream& operator<< (ostream &out,Person &p)
{                 
	cout << "m_a=" << p.m_num << endl;
	cout << "m_b=" << p.m_num1 << endl;
	return out;
}
void test01()
{
	Person p;
	cout << ++(++p) << endl;//该形式只能重载前置,函数内写的是后置,结果也是前置,甚至不能P++
	cout << p << endl;//都输出2 102
	//cout << p++ << endl;//没有找到接受“Person”类型的右操作数的运算符(或没有可接受的转换)
	//cout << p << endl;
}
void test02()
{
	Person p;
	cout << p++ << endl;//没有找到接受“Person”类型的右操作数的运算符(或没有可接受的转换)
	cout << p << endl;
}
int main()
{
	test01();
	test02();
	cout << 10 << endl;
	system("pause");
	return 0;
}
02. 一个成员变量,后置BUG ——到底为什么!

所以是哪里出了问题,不仅不能输出,也没有加上!!!!!!啊啊啊啊

#include<iostream>
using namespace std;
class Person
{
	friend ostream& operator<< (ostream& out, Person& p);
public:
	Person()
	{
		m_num = 0;
	}
	Person operator++ (int)//int 代表占位参数,可以用于区分前置和后置递增
	{
		//先返回结果,再递增
		Person temp= *this;
		return temp;//temp是局部变量,函数结束就被释放,返回局部变量是非法操作,这里是返回值
		(this->m_num)++;
		//最后返回递增结果
		
	}
private:
	int m_num;
};

ostream& operator<< (ostream &out,Person &p)
{                 
	cout << "m_a=" << p.m_num << endl;
	return out;
}

void test02()
{
	Person p1;
	cout << p1 << endl;
	p1++;
	cout << p1 << endl;
}
int main()
{
	test02();
	cout<<10<<endl;
	system("pause");
	return 0;
}
0.3 照着视频打的,错误——所以为什么我的错了啊啊啊啊啊,我检查不出来
#include<iostream>
#include<string>
using namespace std;
class MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger& myint);
	friend void test01();
public:
	MyInteger()
	{
		m_Num = 0;
	}
	MyInteger operator++(int)
	{
		MyInteger temp = *this;
		m_Num++;
		return temp;
	}
private:
	int m_Num;
};
ostream& operator<<(ostream& cout, MyInteger& myint)
{
	cout << myint.m_Num;
	return cout;
}
void test01()
{
	MyInteger mint;
	cout << mint++ << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}
 0.4 复制的,正确——到底为什么!!!!
#include <iostream>
#include <string>
using namespace std;

class MyInteger{
    friend ostream & operator<<(ostream & cout, MyInteger myint);
    public:
    MyInteger()
    {
        m_Num = 0;
    }
    // 重载后置++
    MyInteger operator++(int)
    {
        MyInteger temp = *this;
        m_Num++;
        return  temp;
    }
    private:
    int m_Num;
};
// 重载cout
ostream & operator<<(ostream & cout, MyInteger myint){
    cout << myint.m_Num;
    return cout;
}

void test02()
{
    MyInteger myint;
    cout << myint++ << endl;
    cout << myint << endl;
}

int main(){
    test02();
    system("pause");
    return 0;
}
00001 我把照着视频打的函数部分,粘贴到正确的,能运行,但是有提示“没有与这些操作数匹配的<<运算符
#include <iostream>
#include <string>
using namespace std;

class MyInteger {
    friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
    MyInteger()
    {
        m_Num = 0;
    }
    // 重载后置++
  /*  MyInteger operator++(int)
    {
        MyInteger temp = *this;
        m_Num++;
        return  temp;
    }*/
    MyInteger operator++(int)
    {
        MyInteger temp = *this;
        m_Num++;
        return temp;
    }
private:
    int m_Num;
};
// 重载cout
ostream& operator<<(ostream& cout, MyInteger myint) {
    cout << myint.m_Num;
    return cout;
}

void test02()
{
    MyInteger myint;
    cout << (myint++) << endl;
    cout << (myint++) << endl;
    cout << myint << endl;
}

int main() {
    test02();
    system("pause");
    return 0;
}
00002 我人真的麻了,去你MUA的

#include<iostream>
#include<string>
using namespace std;
class MyInteger
{
	friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
	MyInteger()
	{
		m_Num = 0;
	}
	MyInteger operator++(int)
	{
		MyInteger temp = *this;
		m_Num++;
		return  temp;
	}
	int m_Num;
};
//ostream& operator<<(ostream& cout, MyInteger& myint)
//{
//	cout << myint.m_Num;
//	return cout;
//}
ostream& operator<<(ostream& cout, MyInteger myint) {
	cout << myint.m_Num;
	return cout;
}
void test01()
{
	MyInteger mint;
	cout << mint++ << endl;
	cout << mint << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}
0.5 递减(前置) 
#include<iostream>
#include<string>
using namespace std;
class Person
{
	friend ostream& operator<<(ostream& cout, Person& pp);
public:
	Person()
	{
		m_a = 10;
		m_b = 100;
	}
	Person& operator--()
	{
		m_a--;
		m_b--;
		return *this;//返回自身
	}
private:
	int m_a;
	int m_b;
};
ostream& operator<<(ostream& cout, Person& pp)
{
	cout << pp.m_a << "\t"<<pp.m_b << endl;
	return cout;
}
void test01()
{
	Person pp;
	cout << pp << endl;
	cout << --(--pp) << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}
0.6 递减(前置)——呵呵呵,一摸一样的BUG
#include<iostream>
#include<string>
using namespace std;
class Person
{
	friend ostream& operator<<(ostream& cout, Person& pp);
public:
	Person()
	{
		m_a = 10;
		m_b = 100;
	}
	Person& operator--()
	{
		m_a--;
		m_b--;
		return *this;//返回自身
	}
	Person operator--(int)
	{
		Person temp = *this;
		return temp;
		m_a--;
		m_b--;
	}
private:
	int m_a;
	int m_b;
};
ostream& operator<<(ostream& cout, Person& pp)
{
	cout << pp.m_a << "\t"<<pp.m_b << endl;
	return cout;
}
void test01()
{
	Person pp;
	cout << pp << endl;
	cout << --(--pp) << endl;
}
void test02()
{
	Person pp;
	cout << pp << endl;
	cout << pp-- << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
0.7 正确的,俺小弟的代码……此时我还是不知道我的代码有什么BUG……专业的就是不一样……代码都不偷懒的
#include <iostream>

class MyClass {
private:
    int data;
public:
    MyClass(int num) {
        _data = num;
    }

    // 重载插入运算符 << 作为类的友元函数
    friend std::ostream& operator<<(std::ostream& os, const MyClass& obj) {
        os << "MyClass Object with data: " << obj._data;
        return os;
    }

    // 前缀形式重载
    MyClass& operator--() {
        --_data;
        return *this;
    }

    // 后缀形式重载
    MyClass operator--(int) {
        MyClass temp(*this);
        --_data;
        return temp;
    }

    void display() {
        std::cout << "Count: " << _data << std::endl;
    }

private:
    int _data;
};

int main() {
    MyClass obj(42);
    std::cout << obj << std::endl; // 输出: MyClass Object with data: 42


    std::cout << "Before decrement: ";
    obj.display();

    --obj; // 前缀形式
    std::cout << "After prefix decrement: ";
    obj.display();

    obj--; // 后缀形式
    std::cout << "After postfix decrement: ";
    obj.display();
    return 0;
}
0.8 GPT改了能跑的:Q1:递减之前返回,函数结束,没有递减 Q2:重载函数应常量引用

在后置递减运算符重载函数中,你在返回临时对象之后执行了递减操作,这会导致递减操作失效。正确的做法是在返回临时对象之前执行递减操作。
错误信息提示说没有找到接受"Person"类型的右操作数的运算符,这是因为你的重载函数参数应该是常量引用而不是非常量引用。 

#include<iostream>
#include<string>
using namespace std;
class Person
{
	friend ostream& operator<<(ostream& cout,const Person& pp);
public:
	Person()
	{
		m_a = 10;
		m_b = 100;
	}
	Person& operator--()
	{
		m_a--;
		m_b--;
		return *this;//返回自身
	}
	Person operator--(int)
	{
		Person temp = *this;
		m_a--;
		m_b--;
		return temp;
	}
private:
	int m_a;
	int m_b;
};
ostream& operator<<(ostream& cout,const Person& pp)
{
	cout << pp.m_a << "\t" << pp.m_b << endl;
	return cout;
}
void test01()
{
	Person pp;
	cout << pp << endl;
	cout << --(--pp) << endl;
}
void test02()
{
	Person pp;
	cout << pp << endl;
	cout << pp-- << endl;
	cout << pp << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

4.4 赋值

C++编译器至少给一个类提供4个函数:无参构造,无参析构,默认拷贝,赋值运算符operator=,对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝的问题

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
	int* m_Age;
	Person(int age)
	{
		m_Age = new int(age);//创建堆区的数据,返回指针,m_Age指向堆区数据
	}
	//这样才是真的返回自身,实现链式操作
	Person& operator=(const Person& p)
	{
		if (m_Age != NULL)//先判断是否有数据在堆区,有先释放干净再深拷贝
		{                 //p1=p2的时候,p1指向的堆区已经有内存了,释放
			delete m_Age;
		}
		this->m_Age = new int(*p.m_Age);
		return *this;
	}
	

};
void test01()
{
	Person p1(18);
	std::cout << "p1的年龄为" << *p1.m_Age << endl;
	Person p2(30);
	std::cout << "p2的年龄为" << *p2.m_Age << endl;
	p1 = p2;//简单拷贝,P1,P2都指向同一快内存,重复释放,程序崩溃  解决:深拷贝
	std::cout << "p1的年龄为" << *p1.m_Age << endl;
	std::cout << "p2的年龄为" << *p2.m_Age << endl;
	Person p4(100);
	p1.operator=(p4);//本质
	std::cout << "p1的年龄为" << *p1.m_Age << endl;
	p4 = p1 = p2;
	std::cout << *p2.m_Age << endl << *p1.m_Age << endl << *p4.m_Age << endl;
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}
4.5 关系,>,<,!=,==
#include<iostream>
#include<string>

class Person
{
public:
	int _age;
	std::string _name;
	Person(std::string name,int age) {
		_age = age;
		_name = name;
	}
	bool operator==(const Person& p)
	{
		if (_age == p._age && _name == p._name) {
			return true;
		}
		return false;
	}
	bool operator!=(const Person& p)
	{
		if (_age != p._age || _name != p._name) {
			return true;
		}
		return false;
	}
};
void test01()
{
	Person p1("张三", 12);
	Person p2("王五", 34);
	Person p3("张三", 12);
	if (p1 == p3) {
		std::cout << "p1(张三, 12)  p3(张三, 12)相等" << std::endl;
	}
	if (p1.operator==(p3)) {
		std::cout << "p1(张三, 12)  p3(张三, 12)相等--本质" << std::endl;
	}
	if (p1 != p2) {
		std::cout << "p1(张三, 12)  p2(王五, 34)不相等" << std::endl;
	}
	if (p1.operator!=(p2)) {
		std::cout << "p1(张三, 12)  p2(王五, 34)不相等--本质" << std::endl;
	}
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}
4.6 函数调用

函数调用运算符(),重载后的使用方式非常像函数的调用,也叫仿函数
没有固定写法,非常灵活

#include<iostream>
#include<string>

class Myprint
{
public:
	void operator()(std::string test) {
		std::cout << test << std::endl;
	}
};
class Myadd
{
public:
	int operator()(int a,int c)
	{
		return a + c;
	}
};
void mypp(std::string test) {
	std::cout << test << std::endl;
}
void test01()
{
	Myprint myprint;
	myprint("哈拉雷");//重载的小括号
	mypp("都无法文如其人服务费");//函数调用
	Myadd myadd;
	std::cout << myadd(120, 23) << std::endl;
	//匿名函数对象——匿名对象:类型名(),执行完马上被释放
	std::cout << Myadd()(10,100) << std::endl;
}
int main()
{
	test01();
	//test02();
	system("pause");
	return 0;
}

感觉有被小弟和GPT一起笑到

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值