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) {}
Your declaration
inside class
Fooandare completely unrelated arrays. The latter is a global variable. Also, zero-length arrays are illegal in C++, if you are using gcc, compile with
-pedanticand it should give you a warning.If you were to access the array declared in
Fooyou would use the following:However, you can only initialize members that are
staticoutside the class, so the following code would work: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.