【发布时间】:2012-01-27 21:26:41
【问题描述】:
我想在类中编写一个函数,使用我稍后在该类中定义的运算符。但我不知道如何向运算符显示现在您必须使用 YOUR (x, y)。
(我看到有人在php中使用$this->func_name。但是这里我不知道。
class Point
{
public:
int x;
int y;
bool operator==(Point p)
{
if (x == p.x && y == p.y)
return 1;
return 0;
}
bool searchArea(vector <Point> v)
{
for (int i = 0; i < v.size(); i++)
if (v[i] == /* what ?? */ )
return 1;
return 0;
}
};
int main()
{
//...
.
.
.
if (p.searchArea(v))
//...
}
【问题讨论】:
-
bool operator==(Point p) const -
同样向量应该通过
const vector<Point>&传递,searchArea也应该是一个const函数,并且应该返回true和false。 -
更好:
bool operator==(const Point &p) const
标签: c++ class operator-keyword