0

取れません。g++ コンパイラを使用します。

コード:

#include <iostream>

using namespace std;

typedef void (* FPTR) ();

class Test
{
    void f1()
    {
        cout << "Do nothing 1" << endl;
    }

    void f2()
    {
        cout << "Do nothing 2" << endl;
    }

    static FPTR const fa[];
};

FPTR const Test::fa[] = {f1, f2};

エラー:

test.cpp:22: error: argument of type ‘void (Test::)()’ does not match ‘void (* const)()’
test.cpp:22: error: argument of type ‘void (Test::)()’ does not match ‘void (* const)()’

関数ポインタの定数配列を取得したいだけなので、

fa[0] = f2;

「読み取り専用メンバーの変更 Test::fa」のようなエラーが発生します

4

2 に答える 2

2

f1 and f2 are not function pointers but member-function pointers, so you cannot assign them to an array of function pointers. You could add them to an array of member-function pointers of Test.

于 2012-05-19T18:43:42.373 に答える
2

The compiler is right. The pointer type is void (Test::*)(). Try it:

typedef void (Test::*FPTR)();

FPTR const Test::fa[] = { &Test::f1, &Test::f2 };  // nicer to read!

f1 and f2 are not functions (i.e. free functions), but (non-static) member functions. Those are very different animals: You can call a function, but you cannot just call a member function. You can only call a member function on an instance object, and anything else doesn't make sense.

于 2012-05-19T18:44:44.940 に答える