Black Moses Black Moses - 1 month ago 8
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());}


Use
.*
or
->*
via which pointer...? (
chaos *
Universe pointer?)

Any ideas?

Answer

You need to surround the member function pointer in parenthesis,

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