【问题标题】:I have array of pointers of methods my class, but I cannot call my method. Code are below我的类有一组方法指针,但我不能调用我的方法。代码如下
【发布时间】:2017-11-14 06:08:54
【问题描述】:
#include <iostream>
#include <stdio.h>
#include <stdint.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

 
class FooBar{
public:
        typedef void(FooBar::*OnDio)(void);

    void OnDio0Irq( void ){
        printf("dio0\n");
    };
    void OnDio1Irq( void ){
        printf("dio1\n");
    };
    FooBar(){
        OnDio  dioArray[] = {&OnDio0Irq, &OnDio1Irq};
        
    };
    
    OnDio  *dioArray[2];
private:
            
};
 
int main(int argc, char* argv[]){
        typedef void(FooBar::*OnDio)(void);
        void (FooBar::*foo)(void);
    OnDio *myPtr;
    FooBar *fb = new FooBar();
    
    myPtr = *(&fb->dioArray[0]);
    foo = (OnDio &)(myPtr[0]);
    (foo)();//me need call fb->dioArray[0]()
    (*myPtr)(); // ?
 }

如何从数组中调用函数? 在我的代码中我有错误:

[Error] 必须使用 '.' 或 '->' 来调用 'foo 中的指向成员函数 (...)',例如'(... ->* foo) (...)'

[Error] 必须使用 '.' 或 '->' 来调用指向成员函数的指针 '* myPtr (...)',例如'(... ->* * myPtr) (...)'

【问题讨论】:

标签: c++ arrays class pointers


【解决方案1】:

要调用指向成员函数 (ptmf) 的指针,您需要一个实例和 ptmf,一起使用。

OnDio 已经被定义为指针类型,因此您可能不需要 OnDio 指针。

另外,你在构造函数中初始化一个局部临时变量,而不是“this”实例的 dioArray。

这个答案也很有帮助:C++: Array of member function pointers to different functions

这是您的代码,已更正为通过指向成员函数的指针调用 dio0。

#include <iostream>
#include <stdio.h>
#include <stdint.h>

class FooBar {
public:
    typedef void(FooBar::*OnDio)(void);

    void OnDio0Irq(void) {
        printf("dio0\n");
    };
    void OnDio1Irq(void) {
        printf("dio1\n");
    };
    FooBar() {
        // declaring a local OnDio array just masks the actual member and then it gets tossed
        // need to initialize this instance, not some local temporary
        dioArray[0] = &FooBar::OnDio0Irq;
        dioArray[1] = &FooBar::OnDio1Irq;
    };

    OnDio dioArray[2];
private:

};

int main(int argc, char* argv[]) {
    // need instance
    FooBar fb;
    // need pointer to member function
    FooBar::OnDio func = fb.dioArray[0];
    // call pointer to member function using instance
    (fb.*func)();
}

【讨论】:

    【解决方案2】:
    #include <iostream>
    #include <stdio.h>
    #include <stdint.h>
    
    
    
    class FooBar {
    public:
        typedef void(FooBar::*OnDio)(void);
        OnDio dioArray[2];
        void OnDio0Irq(void) {
            printf("dio0\n");
        };
        void OnDio1Irq(void) {
            printf("dio1\n");
        };
        FooBar() {
            dioArray[0] = &FooBar::OnDio0Irq;
            dioArray[1] = &FooBar::OnDio1Irq;
        };
    
    
    private:
    
    };
    
    int main(int argc, char* argv[]) {
    
        FooBar* fb  = new FooBar();
        for (int i = 0; i < sizeof(fb->dioArray) / sizeof(fb->dioArray[0]); i++)
        {
            (fb->*fb->dioArray[i])();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多