I'm trying to assign function pointers to const class methods, to an array. However, I am experiencing some compiler errors I cannot solve from Googling.
This is what I am trying to do:
#include <iostream>
#include <array>
struct S
{
std::array<bool (*)(int), 2> arr;
bool foo(int a) const
{
std::cout << a << std::endl;
return true;
}
bool foo2(int b) const
{
std::cout << b << std::endl;
return true;
}
S()
{
arr[0] = &S::foo; // Compiler error
arr[1] = &S::foo2; // Compiler error
const bool res1 = arr[0](6); // calls foo ()
const bool res2 = arr[1](7); // calls foo2()
}
};
int main()
{
S s;
return 0;
}
But I get:
<source>:22:18: error: assigning to 'value_type' (aka 'bool (*)(int)') from incompatible type 'bool (S::*)(int) const': different qualifiers (unqualified vs 'const')
arr[0] = &S::foo;
Can anyone advise?
(I need raw pointers and std::array, not std::function)