I'm pretty new to C++ I have this code:
in my class.hpp
class Dummy {
private:
void f1(void);
void f2(void);
void f3(void);
void f4(void);
public:
void caller(std::string id);
};
in my class.cpp
void Dummy::caller( std::string id ) {
// something something about qualifiers requires Dummy::*f instead of just *f
void (Dummy::*f[4])() = {&Dummy::f1, &Dummy::f2, &Dummy::f3, &Dummy::f4};
string v[4] = {"f1", "f2", "f3", "f4"};
for (int i = 0; i < 4; i++) {
if (id == v[i]) {
(*f[i])();
break ;
}
}
}
this (*f[i])()
is valid in C code, but for some reason in C++, it shows me this error which i googled but was unlucky and found nothing that useful, except for std::invoke
which is in C++17 (?), and I'm bound to C++98
The error:
Class.cpp:41:5: error: indirection requires pointer operand ('void (Dummy::*)()' invalid)
(*f[i])();
^~~~~