一个典型的 getter/setter 看起来像这样:
void setName(const std::string& _name) { name = _name; }
std::string getName() const { return name; }
如果成员是这样的函数指针,我该如何做:
void setBotFunc(int(*botFunc)()) { botFunction = botFunc; }
int (*getBotFunc() const)() { return botFunction; }
特别是我不知道如何以“我只会读取而不修改指针(值)”的方式声明 setter。这甚至有意义吗? const int (*botFunct)()
显然被视为返回 const int
的函数。
最佳答案
void setBotFunc( int(* const botFunc)() )
{
botFunction = botFunc;
}
关于c++ - 如何使函数指针常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17908388/