What you're trying to do there is make a parent know about a child class. The function pointers are called via thing. Thing's methods can be called via matter. But matter's functions can't be called via thing, without some casting... I really don't know how good an idea this is, but it does the job (I highlighted the key area in red):
Code:
// thing.h
class thing;
typedef void (thing::*_thing_fptr)();
class thing {
protected:
std::map<std::string, _thing_fptr> mfp;
public:
thing();
virtual void one();
virtual void two();
virtual void three();
virtual void add( const std::string &f_alias, _thing_fptr fptr );
virtual void call( const std::string &func);
};
// thing.cpp
thing::thing(){
mfp["one"] = &thing::one;
mfp["two"] = &thing::two;
mfp["three"] = &thing::three;
}
void thing::one(){
std::cout<<"One called (thing)\n";
}
void thing::two(){
std::cout<<"Two called (thing)\n";
}
void thing::three(){
std::cout<<"Three called (thing)\n";
}
void thing::add( const std::string &f_alias, _thing_fptr fp ) {
mfp[f_alias] = fp;
}
void thing::call( const std::string &func ) {
(this->*(this->mfp[func]))();
}
// matter.h
class matter;
typedef void (matter::*_matter_fptr)();
class matter : public thing {
private:
public:
matter(){}
virtual void one();
virtual void four();
virtual void add( const std::string &f_alias, _matter_fptr fp );
~matter(){}
};
// matter.cpp
void matter::four() {
std::cout<<"Four called (matter)\n";
}
void matter::one() {
std::cout<< "Four called (matter)\n";
}
void matter::add( const std::string &f_alias, _matter_fptr fp ) {
mfp[f_alias] = reinterpret_cast<_thing_fptr>( fp );
}
// main
int main( void ) {
// Add basic
thing t;
t.call("one");
t.call("two");
t.call("three");
// Add alias
t.add( "my_one", thing::one );
t.call( "my_one" );
// child
matter m;
m.add( "fourth_one", matter::four );
m.call( "fourth_one" );
return 0;
}
Edit: Actually. For those who are more in the know than I is that code dangerous? It works, but it's clearly weird and freaky - a child function is being casted to its parent function pointer typedef and child function doesn't exist in its parent.