简体   繁体   English

用C ++中的函数指针成员初始化结构数组

[英]Initializing array of structures with function pointer member in C++

I am having trouble in initializing an array of structures with a function pointer as a member in it. 我在初始化带有函数指针作为其成员的结构数组时遇到麻烦。

class Record
{
    private:
    typedef void (*display_fn_t) ();
    struct record {
        int a;
        display_fn_t disp;
    };
    static const record rec[];

    void disp1() { cout << "Display 1 with a string to display" << endl; }
    void disp2() { cout << "Display 2 with an integer to display" << endl; }

    public:
    int find() { /* logic to find the record comes here */ }
    void display() {
        for (int i = 0; i < 2; i++) {
            rec[i].disp();
        }
    }
}

const Record::record Record::rec[] = {
    { 10, disp1 },
    { 11, disp2 }
};

int main()
{
    Record r;
    if (r.find())
        r.display();
    return 0;
}

When I compile the above code, I am getting the following compilation error: 编译上面的代码时,出现以下编译错误:

mca_record.cpp:56: error: argument of type 'void (Record::)()' does not match 'void (*)()' mca_record.cpp:56:错误:类型为'void(Record ::)()'的参数与'void(*)()'不匹配

To make the call work you must invoke it like this: 要使呼叫正常工作,您必须像这样调用它:

        for (int i = 0; i < 2; i++) {
            (*rec[i].disp)();
        }

And initialize the table this way: 并以这种方式初始化表:

const Record::record Record::rec[] = {
    { 10, &Record::disp1 },
    { 11, &Record::disp2 }
};

Your syntax is wrong and isn't using the appropriate operators. 您的语法错误,并且未使用适当的运算符。

Fixing a multitude of syntax errors, and stripping out the unrelated find operation, then utilizing proper member function pointers and operator ->* gives the following (one of several ways to do this): 修复多种语法错误,并剥离不相关的find操作,然后使用适当的成员函数指针和operator ->*得到以下内容(执行此操作的几种方法之一):

#include <iostream>

class Record
{
private:
    typedef void (Record::*display_memfn_t)();
    struct record
    {
        int a;
        display_memfn_t disp;
    };

    static const record rec[];

    void disp1() { std::cout << "Display 1 with a string to display" << std::endl; }
    void disp2() { std::cout << "Display 2 with an integer to display" << std::endl; }

public:
    void display();
};

const Record::record Record::rec[] =
{
    { 10, &Record::disp1 },
    { 11, &Record::disp2 }
};

void Record::display()
{
    for (size_t i=0; i<sizeof rec/sizeof*rec; ++i)
        (this->*(rec[i].disp))();
}

int main()
{
    Record r;
    r.display();
    return 0;
}

Output 输出量

Display 1 with a string to display
Display 2 with an integer to display

Compare it to your existing code, and not in particular that pointers to member functions are not simply pointers to functions. 将其与现有代码进行比较,不要特别指出,指向成员函数的指针不仅仅是指向函数的指针。 They require different handling and generally different operators to utilize. 它们需要不同的处理方式,并且通常使用不同的操作员。 See here for different methods of member access (both variable and function). 有关成员访问的不同方法(变量和函数), 请参见此处

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM