• Advertisement
Sign in to follow this  

Problem with std::map of member function pointers

This topic is 3578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am implementing a Quake like console to help debugging and I am running into an issue. If I use this it do work: void Exit(const std::vector<std::string>& Tokens) { exit(0); } std::map<std::string, void (*)(const std::vector<std::string>& Tokens)> m_AvailableCommands; m_AvailableCommands["EXIT"] = &Exit; std::map<std::string, void (*)(const std::vector<std::string>& Tokens)>::iterator it = m_AvailableCommands.find(Tokens[0]); if(it != m_AvailableCommands.end()) { it->second(Tokens); } But if I change the function pointer to a member function pointer (see below) I get an error at (*it).second(Tokens): void Console::Exit(const std::vector<std::string>& Tokens) { exit(0); } std::map<std::string, void (Console::*)(const std::vector<std::string>& Tokens)> m_AvailableCommands; m_AvailableCommands["EXIT"] = &Console::Exit; std::map<std::string, void (Console::*)(const std::vector<std::string>& Tokens)>::iterator it = m_AvailableCommands.find(Tokens[0]); if(it != m_AvailableCommands.end()) { it->second(Tokens); } It would be helpfull to benefit from the class here. Someone have a hint for me? Thanks Simon

Share this post


Link to post
Share on other sites
Advertisement
When you call a pointer to member function you need to bind it to an object.

void (Foo::*mem_fn)(void);

Foo f;
Foo * pf = &f;

(f.*mem_fn)();
(f->*mem_fn)();

Share this post


Link to post
Share on other sites
Thanks!

I though it wouldn't work at first since the caller si the owner of the class but, then, thinking that way, the object is "this" so:

(this->*it->second)(Tokens);

did the job. :)

Thanks again!

Simon

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement