如何使函数指针为常量
How to make a function pointer constant
典型的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;
}
相关文章:
- 将常量指针引用绑定到非常量指针
- 如何使用数据对象上的常量指针初始化类
- 为什么我收到"从常量指针到指针的转换无效?
- C++/QT:使用指向私有成员的常量指针作为只读数据共享
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- 为什么C++中没有常量引用,就像常量指针一样?
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 将常量指针强制转换为非常量
- 带有常量指针的矢量构造函数示例
- 什么是常量指针常量引用类型的参数?(const X* const & p)
- 在多线程函数中返回共享的常量指针会导致计时问题吗?
- 常量指针上的混乱
- 使用 static_cast 时指向常量指针的原因
- 如何在C++中定义常量指针数组?
- 测试此指针的常量指针性质
- 当常量指针用作函数的参数时
- C++ - 无法将顶部常量指针分配给另一个非常量指针
- 在不同类型之间转换常量指针
- 解构常量指针?
- 为什么 std::vector<>::const_reference 可以转换为非常量指针?