Black Moses Black Moses - 1 year ago 86
C++ Question

array of pointers to class method error c++11

I got a small "problem" with array of pointers to class method.

In short:
My class Complex has four functions -

double funX(void)

double fun1(void) const {...}
double fun2(void) const {...}

Then I have and array of pointers to member functions of above recipe.

double (Complex::*arr_ptr_fun[4])(void) const;

I initialize this array in constructor initializer list:

... : re(_re), im(_im), arr_ptr_fun{&fun1,&fun2,&fun3,&fun4} { /*EMPTY*/ }

When I try to call any of these 4 functions via this array e.g.:

std::cout << this->*arr_ptr_fun[0]();

I get an error I do not understand:

error: must use '.*' or '->*' to call pointer-to-member function in '((const Complex*)this)->Complex::arr_ptr_fun[0] (...)', e.g. '(... ->* ((const Complex*)this)->Complex::arr_ptr_fun[0]) (...)'
double fun4(void) const {std::cout << this->*arr_ptr_fun[0](); return sqrt(fun3());}

via which pointer...? (
chaos *
Universe pointer?)

Any ideas?

Answer Source

You need to surround the member function pointer in parenthesis,

std::cout << (this->*arr_ptr_fun[0])();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download