为什么运算符<是非成员函数? [英] Why should operator< be non-member function?

查看:103
本文介绍了为什么运算符<是非成员函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得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?)

但是(零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 operatornon-member function?
  • 还有其他情况使member functionnon-member function有所不同吗?
  • In first case, should I use member function instead of non-member function?
  • Why C++ Primer suggests binary operators be non-member function?
  • Is there any other case that member function and non-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.

这篇关于为什么运算符&lt;是非成员函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆