为什么运算符<是非成员函数? [英] Why should operator< be non-member function?
问题描述
我记得C++ Primer
告诉我们operator<
应该是non-member function
,并且我始终遵守规则.但是现在我想知道原因.
I remeber C++ Primer
tells us operator<
should be non-member function
, and I always obey the rule. But now I want to know the reason.
我写了以下代码:
#include <iostream>
using std::cout;
using std::endl;
struct Point1
{
int x, y;
Point1(const int a, const int b): x(a), y(b) { }
};
inline bool operator<(const Point1& lhs, const Point1& rhs)
{
return lhs.x < rhs.x || (lhs.x == rhs.x && lhs.y < rhs.y);
}
struct Point2
{
int x, y;
Point2(const int a, const int b): x(a), y(b) { }
bool operator<(const Point2& rhs)
{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
};
int main()
{
Point1 a(1, 2), b(1, 3);
cout << (a < b) << " " << (b < a) << endl;
Point2 c(2, 3), d(2, 4);
cout << (c < d) << " " << (d < c) << endl;
}
在这种情况下,似乎它们没有什么区别,并且member
函数似乎更简单.
In this case, It seems they don't make difference and member
function seems much simpler.
但是在这种情况下:
#include <iostream>
using std::cout;
using std::endl;
// Usually I write it for comparing floats
class Float1
{
long double _value;
public:
static const long double EPS = 1e-8;
Float1(const long double value): _value(value) { }
const long double Get() const { return _value; }
};
inline bool operator<(const Float1& lhs, const Float1& rhs)
{
return rhs.Get() - lhs.Get() > Float1::EPS;
}
inline bool operator<(const Float1& lhs, const long double rhs)
{
return rhs - lhs.Get() > Float1::EPS;
}
class Float2
{
long double _value;
public:
static const long double EPS = 1e-8;
Float2(const long double value): _value(value) { }
const long double Get() const { return _value; }
bool operator<(const Float2& rhs)
{
return rhs._value - _value > Float2::EPS;
}
bool operator<(const long double rhs)
{
return rhs - _value > Float2::EPS;
}
};
int main()
{
Float1 x(3.14);
Float2 y(2.17);
long double zero = .0;
cout << (x < zero) << " " << (zero < x) << endl;
//cout << (y < zero) << " " << (zero < y) << endl; Compile Error!
}
(x< 0)和(zero< x)都可以工作! (long double
转换为Float
吗?)
Both (x < zero) and (zero < x) work! (is long double
converted to Float
?)
但是(零
在第一种情况下,您看到member function
的代码长度更短,而在第二种情况下,non-member function
使得比较更加容易.所以我想知道
You see, in first case, member function
costs less code length, and in second case, non-member function
makes comparing easier. So I want to know
- 在第一种情况下,我应该使用
member function
代替non-member function
吗? - 为什么
C++ Primer
建议binary operator
是non-member function
? - 还有其他情况使
member function
和non-member function
有所不同吗?
- In first case, should I use
member function
instead ofnon-member function
? - Why
C++ Primer
suggestsbinary operator
s benon-member function
? - Is there any other case that
member function
andnon-member function
make difference?
感谢您的帮助!
推荐答案
我认为基本答案是非成员函数在隐式转换中发挥更好的作用.因此,如果您可以将二进制运算符编写为非成员函数,则应该这样做.
I think the basic answer is that non-member functions play better with implicit conversion. So if you can write your binary operator as a non-member function, you should.
这篇关于为什么运算符<是非成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!