Pointer-to-private-member-function array

c++, function-pointers

Solution

Your declaration

void (Foo::*someMethods[])(int);

inside class `Foo` and

void (Foo::*someMethods[])(int) = {&Foo::foo1, &Foo::foo2};

are completely unrelated arrays. The latter is a global variable. Also, zero-length arrays are illegal in C++, if you are using gcc, compile with `-pedantic` and it should give you a warning.

If you were to access the array declared in `Foo` you would use the following:

void (Foo::*(Foo::someMethods)[])(int) = {&Foo::foo1, &Foo::foo2};

However, you can only initialize members that are `static` outside the class, so the following code would work:

class Foo {
    public:
        Foo();

    private:
        static void (Foo::*someMethods[])(int);
        void foo3(int);
        void foo1(int);
        void foo2(int);

};

Foo::Foo() {}

void (Foo::*(Foo::someMethods)[])(int) = {&Foo::foo1, &Foo::foo2};

void Foo::foo1(int) {}
void Foo::foo2(int) {}
void Foo::foo3(int) {}

int main(){}

Or your other option is to move the non-static member in the constructor as you said in the comments. But, you should add the size of the array to be conforming.

Problem

I am trying to make an array of pointer to private member functions. The array itself is private, so I don't see why it says: error: ‘void Foo::foo1(int)’ is private This works: ``` class Foo { public: Foo(); void foo1(int); void foo2(int); private: void (Foo::*someMethods[])(int); void foo3(int); }; Foo::Foo() {} void (Foo::*someMethods[])(int) = {&Foo::foo1, &Foo::foo2}; void Foo::foo1(int) {} void Foo::foo2(int) {} void Foo::foo3(int) {} ``` This does not work: ``` class Foo { public: Foo(); private: void (Foo::*someMethods[])(int); void foo1(int); void foo2(int); void foo3(int); }; Foo::Foo() {} void (Foo::*someMethods[])(int) = {&Foo::foo1, &Foo::foo2}; void Foo::foo1(int) {} void Foo::foo2(int) {} void Foo::foo3(int) {} ```

Original source