c++ - 我们如何在该类中使用运算符?

标签 c++ class operator-keyword

我想在类中编写一个函数,使用我之前在该类中定义的运算符。但我不知道如何向运算符显示现在您必须使用 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))
       //...
}

最佳答案

你在哪里有 /* what ?? */ 你想要 *this

关于c++ - 我们如何在该类中使用运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040303/

相关文章:

c++ - 数组的大小 C++

java - 接口(interface)和抽象类广告方法覆盖

arrays - Powershell 如何重载数组索引运算符?

c++ - 二进制 '=' : no operator found which takes a right-hand operand (or there is no acceptable conversation)

c++ - 将内存保存到文件并加载它而无需解析数据?

c++ - 运算符不应该是常量,但可以是

c++ - 带有 aecho 的 FFmpeg 过滤器配置无法配置所有链接和格式 - avfilter_graph_config

python - 如何从解释器堆栈中获取绑定(bind)方法的类名?

C++ vector 模板运算符 []

C++ 迭代器运算符定义