如何在"this"上调用运算符()?
How do I call operator() on "this"?
我有一个成员函数,它需要在类实例(this(上调用operator((,我猜不到正确的语法。我试过
this();
*this();
this->();
this->operator();
还有一些其他的事情,但错误信息不是很丰富,所以我不知道我做错了什么。
我在SE上找到的最接近的问题是:如何调用模板化运算符((?
(*this)(/*parameters*/)
可能是最清晰的方式。
答案:使用
this->operator()();
我提出了一个例子(测试方法(:
#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;
}
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 使用运算符调用 void 函数时出错<set>
- C++:使用运算符 = 调用多参数构造函数
- 重载模板运算符调用单独的类运算符
- 为什么转换运算符调用复制构造函数两次,而等效函数只调用它一次
- C++ - 从移动分配运算符调用复制分配
- 从赋值运算符调用构造函数
- C++ 为什么运算符>会中断嵌套的运算符()调用?
- 显式运算符 = 调用和 = 运算符之间是否有区别
- 全局新运算符调用语法
- (C++)从派生类运算符调用基运算符
- 初始化对象后,用隐式转换而不是赋值运算符调用构造函数有什么意义
- 函数可以使用点运算符调用另一个功能
- 为什么这个运算符=调用不明确
- 对重载运算符调用QMetaObject
- 重载unique_ptr运算符调用make_unique
- 对于什么对象指针值,指向成员的指针运算符调用未定义的行为
- 从重载的下标运算符调用虚函数
- 从运算符调用纯虚函数<<
- 可变参数扩展是否可以用作逗号运算符调用链