C arduino:为函数指针分配功能
C++ Arduino: assign a Function to function pointer
我有一个包含两个类似非静态功能的类,我想即时将其中一个分配给函数指针:
A.H:
class A{
private:
void (*funcPtr)(int);
void do_sth(int i);
void do_sth_else(int i);
}
A.CPP:
A::A(int i){
if(i == 1)
A::funcPtr = A::do_sth; // or &A::do_sth;
else
A::funcPtr = A::do_sth_else; // or &A::do_sth_else;
}
但是我遇到了这样的错误:
error: cannot convert 'A::do_sth' from type 'void (A::)(int)' to type 'void (*)(int)'
我读了多个类似的问题,但不能将其解决方案应用于我的问题。
这些是成员函数。通过将funcPtr
作为成员指针作为成员指针,通过使用类名来限定,或制作do_sth()
,do_sth_else()
静态成员函数。我建议在获取地址时在功能名称前使用&
。
相关文章:
- 将指针分配给另一个指针时会发生什么情况?
- C++将一个指针分配给另一个指针时执行的类型检查
- 我似乎无法为指针分配一个数组,然后更改数组的内容
- C++为 void 指针分配一个字符串
- 当我在 C++ 中将派生类的指针分配给指针时,地址会更改
- MPI 集合通信中的指针分配
- 函数指针分配在创建 DLL 后导致错误
- 通过双指针分配指针
- C++编译时使用 constexpr 字符数组指针分配静态数组?
- 我可以直接为指针分配地址吗?如果是,如何做到这一点
- 为什么为char指针分配内存不会截断char序列
- C++指针分配字符数组的地址
- 将 void 指针分配给另一个 void 指针
- 如何将不同的成员函数指针分配给注册类的不同实例?
- 如何为指针分配特定的内存地址?
- C++ - 无法将顶部常量指针分配给另一个非常量指针
- 将指针分配给另一个指针时会发生什么情况?
- 为指向C++中的对象/对象的指针分配堆内存
- 将指针分配给字符串变量中包含的地址
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用