C++ 成员函数用 &(& 符号)重载

标签 c++ c++11 overloading ampersand

如何选择正确的error()成员函数?我需要以某种方式转换吗?

using namespace std;

struct test
{
   int error();
   void error(int x);
   int fun();
};

int main() {
   auto f1 = &test::error; // how to pick the correct function?
   auto f2 = &test::fun; // works
}

最佳答案

你可以明确指定成员函数指针类型。

int (test::*f1)() = &test::error;
void (test::*f2)(int) = &test::error;

关于C++ 成员函数用 &(& 符号)重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35984788/

相关文章:

c++ - 用 std 实现替换 boost

c++ - 头文件和 cpp 文件中的运算符重载

c++ - std::string 的奇怪行为

c++ - "Must construct a QApplication before a QWidget"

c++ - iterator 和 const_iterator 之间的比较是否效率低下?

Java方法重载和调用

c# - 如果我选择不同的通用约束,为什么不能重载具有相同参数的方法?

c++ - 为什么将对象插入 vector 会导致 C2679

android - C++ 服务器使用 TCP 向 Android 客户端发送消息

c++ - 如何使用来自不同继承层次级别的多个虚函数指针作为模板参数?