如何使函数指针为常量

How to make a function pointer constant

本文关键字:常量 指针 函数 何使      更新时间:2023-10-16

典型的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。这有道理吗?CCD_ 1显然被当作一个返回CCD_。

void setBotFunc( int(* const botFunc)() )
{
    botFunction = botFunc;
}