简体   繁体   English

重载大于有或没有朋友的运算符

[英]overload greater than operator with or without friend

Suppose I have the following class: 假设我有以下课程:

class Point{
private:
int x,y;

public:
int get_x() const {return x;}
int get_y() const {return y;}

Point() :x(0),y(0){}
Point(int x,int y):x(x),y(y){}
Point(const Point& P){
    x = P.get_x();
    y = P.get_y();
}
Point& operator=   (const Point& P) {
            x = P.get_x();
            y = P.get_y();

    return *this;
}
friend ostream& operator<<(ostream& os,const Point& P) {

    os<<"["<<P.get_x()<<", "<<P.get_y()<<"]";
    return os;
}


Point operator - (const Point &P){
    return Point(x-P.get_x(),y-P.get_y());
}

friend bool operator > (const Point &A, const Point &B) {
    return A.get_y()>B.get_y();
}


};

Here I used friend function. 在这里我使用了朋友功能。 I can also use function without friend: 我也可以在没有朋友的情况下使用功能:

class Point{
...
bool operator > (const Point &B) const {
    return y>B.get_y();
}
 ...

};

What are the differences between them in actual implementations? 它们在实际实现中有什么区别? Also in the second method, the code won't compile without 'cont', why is that? 同样在第二种方法中,如果没有'cont',代码将无法编译,为什么呢? Even after I changed the getter function into non-const function, it still won't compile without the 'const'. 即使在我将getter函数更改为非const函数之后,如果没有'const',它仍然无法编译。

As you've already noticed, comparison operator overloads can either be implemented as a member function or as a non-member function. 正如您已经注意到的,比较运算符重载可以实现为成员函数或非成员函数。

As a rule of thumb you should implement them as a non-member non- friend function where possible, as this increases encapsulation, and it allows (non- explicit ) conversion constructors to be used on either side of the operator. 根据经验 ,应尽可能将它们实现为非成员非friend函数,因为这会增加封装,并且允许在运算符的任何一侧使用(非explicit )转换构造函数。

Say for instance your Point class for whatever reason had an int conversion constructor: 举例来说,无论出于何种原因,您的Point类都具有int转换构造函数:

Point(int x);

With a non-member comparison operator you can now do the following: 使用非成员比较运算符,您现在可以执行以下操作:

Point p;
p < 3; // this will work with both a member and non-member comparison
3 < p; // this will **only** work if the comparison is a non-member function

You also seem to be confused about when to use const , again as a rule of thumb for comparison operators you should always use const wherever possible, because comparisons logically do not involve any change to the object. 同样,对于何时使用const似乎也感到困惑,同样,作为比较运算符的经验法则,应该始终尽可能使用const ,因为逻辑上比较不会涉及对象的任何更改。

As Point is a very small class you could also take it by value instead, so in order of most to least preferable your options are: 由于Point是一个很小的类,因此您也可以按值取值,因此,按从高到低的顺序,您可以选择:

// Non-member, non-friend
bool operator>(Point const& A, Point const& B);
bool operator>(Point A, Point B);

// Non-member, friend    
friend bool operator>(Point const& A, Point const& B);
friend bool operator>(Point A, Point B);

// Member
bool Point::operator>(Point const& B) const;
bool Point::operator>(Point B) const;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM