C++运算符重载需要注意的地方

有时候自己写一个类,需要重载运算符,但对于一些操作符(如==<>等),可以在类里写,也可以在类外写。那么 C++ 编译器会去怎么调用呢?


首先,我们知道,“重载” 机制可以允许多个函数有相同的函数名,但参数列表必须不同。C++编译器经过名字改编(name mangling),可以在调用时依据所传实参找到“最符合”的函数实体进行调用。而运算符重载亦是如此。


首先,我们有一个类:

class CMyCls
{
public:
	bool operator == (const CMyCls& rhs);				// 1.
	
	bool operator == (const CMyCls& rhs) const;			// 2.

	bool operator == (CMyCls& rhs);						// 3.

	bool operator == (CMyCls& rhs) const;				// 4.
};


void f1 (CMyCls& lhs, const CMyCls& rhs);
{
	lhs == rhs;
}

void f2 (const CMyCls& lhs, const CMyCls& rhs);
{
	lhs == rhs;
}

void f3 (CMyCls& lhs, CMyCls& rhs);
{
	lhs == rhs;
}

void f4 (const CMyCls& lhs, CMyCls& rhs)
{
	lhs == rhs;
}

那么 f1 - f4 这四个函数将会如何调用呢?其实,f1函数会调到CMyCls类中1 处注释所标的函数,f2会调到 2 处注释所标的函数,f3f4依次类推。


那么,如果我在类里面有一个运算符重载,在类外也有,会调用哪一个呢?比如:

class CMyCls
{
public:
	bool operator == (const CMyCls& rhs) const
	{
		// ...
	}
};


bool operator == (const CMyCls& lhs, const CMyCls& rhs)
{
	// ...
}


void f(const CMyCls& lhs, const CMyCls& rhs)
{
	lhs == rhs;
}

但很不幸的是,这样编不过。因为对于 f 函数的调用来说,编译器不知道调哪一个会比较好,因为同时存在两份“最适合”的重载函数,所以产生歧义。但这样是可以编过的,思考下为什么:

class CMyCls
{
public:
	bool operator == (const CMyCls& rhs)
	{
		// ...
	}
};


bool operator == (const CMyCls& lhs, const CMyCls& rhs)
{
	// ...
}


void f1(const CMyCls& lhs, const CMyCls& rhs)
{
	lhs == rhs;
}


void f2(CMyCls& lhs, const CMyCls& rhs)
{
	lhs == rhs;
}

上面 f1会调到全局的函数operator ==,而f2会调到 CMyCls的成员函数operator ==。所以,最好的写法是只在类里或类外重载两个参数均为const的版本。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值