重载的操作符成员函数与非成员函数

今天看effective STL第26条条款时,发现自己之前没意识到如下的几点(看来自己的c++基础还是渣渣水平o(╯□╰)o)。

如果一个类重载操作符,当在全局域内也重载了同样的操作符,那么在调用该操作符时会面临选择问题。

于是自己test了一下,发现在同等条件下,优先选择类中的操作符重载函数。(想想也挺合理的)

作为类的成员函数这一点在进行重载函数选择时是一个优先的,因此注意某些情况会导致调用重载函数时编译器无法抉择哪一个重载函数。

以下是test代码:

#include <iostream>
#include <list>
#include <stack>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
using namespace std;

class A{
public:
	A(int value){
		v = value;
	}
	int V() const{
		return v;
	}
	bool operator==(const A &x){
		cout << "member function called!" << endl;
		return v==x.V();
	}
private:
	int v;
};

class B{
public:
	B(int value){
		v = value;
	}
	int V()const{
		return v;
	}
	bool operator==(const A &x){
		cout << "member function of B called" << endl;
		return v==x.V();
	}
private:
	int v;
};


class C{
public:
	C(int value){
		v = value;
	}	
	int V() const {
		return v;
	}
	bool operator==(const int &x){
		cout << "member function of C called" << endl;
		return v==x;
	}
private:
	int v;
};

bool operator==(const C &x,const double &y){
	cout << "global function called" << endl;
	return x.V()==y;
}

bool operator==(const C &x,const int &y){
	cout << "global function called" << endl;
	return x.V() == y;
}

bool operator==(const A &x,const A &y){
	cout << "global function called" << endl;
	return x.V()==y.V();
}

int main(){

	A ta(5);
	A tb(5);
	ta == tb;

	B ca(5);
	ca == ta;//调用==左边类的operator==

	C tc(5);
	/*
	const double tmp = 5;
	tc==tmp;
	编译不过,因为编译器无法抉择选择哪个类C中的==还是全局中的==
	*/<( ̄ˇ ̄)/
	const int tmp = 5;
	tc==tmp;//输出member function of C called

	system("pause");
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用友元函数来实现双目操作符重载,但需要注意一些细节问题。 首先,友元函数不能直接访问类的私有成员,因此需要将双目操作符重载函数声明为类的成员函数或者将其声明为友元函数。 其次,如果将双目操作符重载函数声明为类的成员函数,则该函数的左操作数将自动绑定到调用该函数的对象上,而右操作数需要作为函数的参数传递。 如果将双目操作符重载函数声明为友元函数,则需要在函数内部访问类的私有成员,可以通过将该函数声明为类的友元函数来实现。 以下是一个示例代码,演示了如何使用友元函数实现双目操作符重载: ```C++ #include <iostream> class Complex { public: Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} Complex operator+(const Complex& other) const { return Complex(real + other.real, imag + other.imag); } double getReal() const { return real; } double getImag() const { return imag; } private: double real, imag; }; Complex operator-(const Complex& lhs, const Complex& rhs) { return Complex(lhs.getReal() - rhs.getReal(), lhs.getImag() - rhs.getImag()); } int main() { Complex c1(1.0, 2.0), c2(3.0, 4.0); Complex c3 = c1 + c2; Complex c4 = c1 - c2; std::cout << "c1 + c2 = " << c3.getReal() << " + " << c3.getImag() << "i" << std::endl; std::cout << "c1 - c2 = " << c4.getReal() << " + " << c4.getImag() << "i" << std::endl; return 0; } ``` 在上面的示例代码中,`operator-`函数被声明为一个友元函数,并且直接访问了类的私有成员。在`main`函数中,我们使用`operator+`和`operator-`来进行复数的加减操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值