C arduino:为函数指针分配功能

C++ Arduino: assign a Function to function pointer

本文关键字:指针 分配 功能 函数 arduino      更新时间:2023-10-16

我有一个包含两个类似非静态功能的类,我想即时将其中一个分配给函数指针:

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()静态成员函数。我建议在获取地址时在功能名称前使用&