运算符重载(1)

本文介绍了C++中加号、左移和递增运算符的成员函数和全局函数重载,以及它们在实际编程中的应用和注意事项。通过实例演示了如何使用这些重载函数简化代码并理解其内部机制。
摘要由CSDN通过智能技术生成

1.加号运算符重载,这里用编译器统一的名称operator代替函数名

#include<iostream>
using namespace std;
//1.成员函数的加号重载
//2.全局函数的加号重载
class Person
{
public:
	Person() {};
	//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(int a ,int b)
	{
		this->m_A = a;
		this->m_B = b;
	}

	int m_A;
	int m_B;
};

//2.全局函数的加号重载
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 p3;
	Person p1(10,10);
	Person p2(10,10);


	//1.成员函数加号重载实质
	//p3=p1.operator+(p2);
	//cout << p3.m_A << " " << p3.m_B << endl;
	//当实现了加号重载,就可以简化写成这样


	//2.全局函数加号重载实质
	p3 = operator+(p1, p2);
	cout << p3.m_A << " " << p3.m_B << endl;

	p3 = p1 + p2;
	cout << p3.m_A << " " << p3.m_B << endl;
}
int main()
{
	test01();
}

2.左移运算符重载

1.成员函数重载(行不通),会导致p在左,cout在右的问题

//全局函数左移重载
//这里cout的函数类型是ostream,这里cout前加引用是为了保证cout的唯一,引用即给cout起别名的意思
//为了能够连续cout<<...<<...<<...返回值必须是cout

#include<iostream>
using namespace std;

class Person
{
	friend ostream& operator<<(ostream& cout, Person& p);
public:

	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}
private:
	int m_A;
	int m_B;
};

//全局函数左移重载
//这里cout的函数类型是ostream,这里cout前加引用是为了保证cout的唯一,引用即给cout起别名的意思
//为了能够连续cout<<...<<...<<...返回值必须是cout
ostream& operator<<(ostream& cout, Person& p)
{
	cout << "m_A = " << p.m_A << " " << "m_B = " << p.m_B;
	return cout;
}

void test01()
{	
	Person p(10, 10);
    //由于p是自定义类型的变量,cout并不知道返回什么,因此要自己实现cout函数的返回
	cout << p << endl;
}
int main()
{
	test01();
}

在观看视频,敲代码的时候,我思考为什么Person &一个函数,于是我搜了以下解释:

3.递增运算符重载

#include<iostream>
using namespace std;

class MyInteger
{
    friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
    MyInteger()
    {
        m_Num = 0;
    }

    //1.前置++  这里用引用是为了一直为一个数据进行递增操作
    //例如++(++myint)时,里面myint从1变为了2,导致更换了数据.
    //其实本质就是不用引用返回,每次返回的就是一个新对象,拷贝调用原则
    //防止拷贝构造函数,新的对象会调用新的拷贝构造函数
    //这里面的this指针是形参,出了该函数后,未被释放,它属于这个Myinteger类中,只有这个类执行完后,才会释放
    MyInteger &operator++()
    {
        m_Num++;
        return *this;
    }

    //2.后置++ 这里的是占位参数,用来区分前置++与后置++的
    //如果这里用引用的话&operator++,这里会返回局部变量的引用,该函数调用完后temp被释放,后面再解引用后会造成非法操作了
    MyInteger operator++(int)
    {    
        //先 记录当前的结果
        MyInteger temp = *this;
        //后递增
        m_Num++;
        return temp;
    }

private:
    int m_Num;
};

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

void test02()
{
    MyInteger myint;
    cout << myint++ << endl;
    cout << myint << endl;
}
ostream& operator<<(ostream& cout, MyInteger myint)
{
    cout << "m_Num的值为: " << myint.m_Num;
    return cout;
}

int main()
{
    /*test01();*/
    test02();
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值