我已经为Point2D类实现了两个运算符重载
- 运营商LT;
- 运营商GT;
醇>
在头文件中我需要声明为友元函数,否则我会收到编译错误 问题:运算符重载函数是否始终是友元函数?它可以是静态的吗? 我尝试使用静态函数,但有一个错误:
Point2D.h:19:58: error: ‘static bool Point2D::operator<(const Point2D&, const Point2D&)’ must be either a non-static member function or a non-member function
In file included from Point2D.cpp:3:0:
Point2D.h:19:58: error: ‘static bool Point2D::operator<(const Point2D&, const Point2D&)’ must be either a non-static member function or a non-member function
Point2D.h: In function ‘bool operator<(const Point2D&, const Point2D&)’:
Point2D.h:23:7: error: ‘int Point2D::x’ is protected
Point2D.cpp:36:11: error: within this context
Point2D.h:24:7: error: ‘int Point2D::y’ is protected
Point2D.cpp:36:17: error: within this context
class Point2D
{
friend ostream &operator<<( ostream &output, const Point2D &P);
friend bool operator<(const Point2D& A, const Point2D& B);
friend bool operator>(const Point2D& A, const Point2D& B);
protected:
int x;
int y;
public:
//Constructor
Point2D();
Point2D (int x, int y);
//Accessors
int getX();
int getY();
//Mutators
void setX (int x);
void setY (int y);
};
ostream &operator<<( ostream &output, const Point2D &P)
{
output << "X : " << P.x << "Y : " << P.y;
return output;
}
bool operator<(const Point2D& A, const Point2D& B )
{
return A.x < B.y;
};
bool operator>(const Point2D& A, const Point2D& B )
{
return A.x > B.y;
};
答案 0 :(得分:2)
二元运算符可以实现为全局二元函数或一元成员函数。在后者中,运算符的第一个操作数是隐式传递的(Is *this
,与所有成员函数一样):
struct foo
{
int a;
};
bool operator==( const foo& lhs , const foo& rhs )
{
return lhs.a == rhs.b;
}
struct foo
{
int a;
bool operator==( const foo& rhs )
{
// +---- this->a
// |
// v
return a == rhs.a;
}
};
struct foo
{
int a;
friend bool operator==( const foo& lhs , const foo& rhs )
{
return lhs.a == rhs.b;
}
};
有关运算符重载的完整且深入的指南,请查看此C ++ wiki主题:Operator overloading
答案 1 :(得分:0)
将重载功能更改为:
friend ostream &operator<<( ostream &output, const Point2D &);
bool operator<(const Point2D&);
bool operator>(const Point2D&);
bool operator<(const Point2D& B )
{
return x < B.x && y < B.y;
}
bool operator>(const Point2D& B )
{
return x > B.x && y > B.y;
}
friend ostream &operator<<( ostream &output, const Point2D &P)
{
output << "X : " << P.x << "Y : " << P.y;
return output;
}
或者,您可以将它们设为所有朋友功能:
friend ostream &operator<<( ostream &output, const Point2D &);
friend bool operator<(const Point2D&, const Point2D&);
friend bool operator>(const Point2D&, const Point2D&);
friend bool operator<(const Point2D& A, const Point2D& B )
{
return A.x < B.x && A.y < B.y;
}
friend bool operator>(const Point2D& A, const Point2D& )
{
return A.x > B.x && A.y > B.y;
}
friend ostream &operator<<( ostream &output, const Point2D &P)
{
output << "X : " << P.x << "Y : " << P.y;
return output;
}
通常,如果您希望该函数可以访问该类的私有成员,则可以创建一个朋友重载。如果您不需要它可以访问私有成员,那么只需剥离好友标签并在课堂外声明该功能。
答案 2 :(得分:0)
您的比较运算符不一定是朋友,除非您需要访问无法访问的非公共成员。在这种情况下,您可以使用访问者功能getX()
和getY()
像:
// Have a declaration for operator < after the Point2D class in the header file. Not inside as a friend.
// Global operator <
bool operator<(const Point2D& A, const Point2D& B )
{
return A.getX() < B.getX();
};
值得注意的是,比较运算符通常是该类的成员。流操作运算符,如<<
&amp; >>
是非成员,可选friend
。但是,并不总是需要使比较运算符成为成员函数。