重载大于有或没有朋友的运算符 [英] overload greater than operator with or without friend

查看:45
本文介绍了重载大于有或没有朋友的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下课程:

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();
}


};

在这里我使用了朋友功能.我也可以在没有朋友的情况下使用功能:

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

};

它们在实际实现中有什么区别?同样在第二种方法中,如果没有"cont",代码将无法编译,这是为什么呢?即使在我将getter函数更改为非const函数之后,如果没有'const',它仍然无法编译.

解决方案

您已经注意到,比较运算符重载可以实现为成员函数或非成员函数.

作为经验法则,您应该在可能的情况下将它们实现为非成员non- friend函数,因为这会增加封装,并且允许在运算符的任何一侧使用(non- explicit)转换构造函数.

例如,无论出于何种原因,您的Point类都具有int转换构造函数:

Point(int x);

使用非成员比较运算符,您现在可以执行以下操作:

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

您似乎也对何时使用const感到困惑,再次作为比较运算符的经验法则,应始终尽可能使用const,因为逻辑上比较不会涉及对象的任何更改.

由于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;

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? Even after I changed the getter function into non-const function, it still won't compile without the '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.

Say for instance your Point class for whatever reason had an int conversion constructor:

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.

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:

// 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;

这篇关于重载大于有或没有朋友的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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