运算符的重载

本文详细介绍了C++中的运算符重载概念,包括双目运算符、单目运算符及输入输出运算符的重载方法。通过具体示例,讲解了如何通过成员函数和全局函数实现运算符重载,以及在重载过程中如何处理const对象和私有成员访问的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

双目运算符的重载

成员函数:
类对象 operator#(const 类& that) const
{
	return 类(参数#参数);
}
注意:双目运算符的运算结构是个右值,返回值应该加 const,然后为了 const 对象能够调用参数应该写 const,函数也应该具备 const 属性。

全局函数:
类 operator#(const 类& a,const 类& b)
{

}
注意:全局函数不是成员函数,可能会访问到类的私有成员,解决这种问题可以把函数声明为类的友元函数(友元不是成员)。
友元:在类的外部想访问类的私有成员时,需要所在的函数声明为友元,但友元只是朋友,因此它只有访问权限,没有实际的拥有权(其根本原因是它没this指针)。

友元声明:把函数的声明写一份到类中,然后在声明前加上 friend 关系,使用友元既可以把操作符函数定义为全局的,也可以确保类的封装性。

注意:友元函数与成员函数不会构成重载,因为它们不再同一个作用域内。

单目运算符的重载

成员
	const 类 operator#(void) const
	{
	
	}
全局
	const 类 operator#(const 类& that)
	{
	
	}

输入输出操作符重载
cout 是 ostream 类型的对象,cin 是 istream 类型的对象
如果 << / >>运算实现为成员函数,那么调用者应该是ostream/istream,而我们无权增加标准库代码,因此输入/输出运算符只能定义为全局函数。

ostream& operator <<(ostream& os,const 类& n)
{
	
}

istream& operator >>(istream& is,类& n)
{

}

注意:在输入输出过程中,cin/cout 会记录错误标志,因此不能加 const 属性。
cout << 类对象 << endl;

代码实现

#include <iostream>

using namespace std;

class Point
{
	int x;
	int y;
public:
	Point(int _x=0,int _y=0)
	{
		x = _x;
		y = _y;
	}

	friend const Point operator+(const Point& a,const Point& b);

	const Point operator+(const Point& that) const
	{
		return Point(that.x+x,that.y+y);	
	}

	const Point operator-(const Point& that) const
	{
		return Point(x-that.x,y-that.y);
	}
	const Point operator*(const int n) const
	{
		return Point(x*n,y*n);
	}

	friend const Point operator*(const int n,const Point& that);

	/*
	const Point& operator+=(const Point& that)
	{
		x += that.x;
		y += that.y;
		return *this;
	}
	*/
	friend const Point& operator+=(Point& a,const Point& b);

	/*
	const Point operator-(void)
	{
		return Point(-x,-y);
	}
	*/

	friend const Point operator-(const Point& that);
	
	// 前++
	/*Point& operator++(void)
	{
		++x;
		++y;
		return *this;
	}*/
	friend Point& operator++(Point& that);

	// 后++ (哑元)
	/*Point operator++(int)
	{
		Point temp(x,y);
		x++;
		y++;
		return temp;
	}*/
	friend Point operator++(Point& that,int);
	friend ostream& operator<<(ostream& os,const Point& p);
	friend istream& operator>>(istream& is,Point& p);
};

ostream& operator<<(ostream& os,const Point& p)
{
	return os << p.x << "," << p.y;
}

istream& operator>>(istream& is,Point& p)
{
	cout << "请输入x的值:";
	is >> p.x;
	cout << "请输入y的值:";
	is >> p.y;
	return is;
}

Point operator++(Point& that,int)
{
	Point temp(that.x,that.y);
	that.x++;
	that.y++;
	return temp;
}

Point& operator++(Point& that)
{
	that.x++;
	that.y++;
	return that;
}

const Point operator-(Point& that)
{
	return Point(-that.x,-that.y);
}

const Point& operator+=(Point& a,const Point& b)
{
	a.x += b.y;
	a.y += b.y;
	return a;
}

const Point operator*(const int n,const Point& that)
{
	return Point(n*that.x,n*that.y);
}

/*
const Point operator+(const Point& a,const Point& b)

	return Point(a.x+b.x,a.y+b.y);
}
*/
int main()
{
	const Point p(3,9);
	int n = 0;
	n----;
	
	Point p1(1,7);
	cin >> p1;
	cout << p1 << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值