如何在“ this”上调用operator()?

时间:2019-11-20 13:58:31

标签: c++

我有一个成员函数,需要在类实例(this)上调用operator(),我无法猜测正确的语法。 我尝试过

git cz

和其他一些事情,但是错误消息不是很有用,所以我不知道我在做什么错。

我在SE上找到的最接近的数字:How do I call a templatized operator()()?

3 个答案:

答案 0 :(得分:8)

(*this)(/*parameters*/)

可能是最清晰的方法。

答案 1 :(得分:5)

答案:使用

  this();
  *this();
  this->();
  this->operator();

答案 2 :(得分:4)

我提出一个示例(测试方法):

#include <iostream>

class A
{
public:
    int operator()(int index)
    {
        return index + 1;
    }
    int test()
    {
        // call to operator ()
        return this->operator()(5);
    }
};

int main()
{
    A obj;
    std::cout << obj.test() << std::endl;
    std::cout << obj(7) << std::endl;
    std::cout << obj.operator()(9) << std::endl;
}