EOSERV Forum > Programming > Function pointers to non static member functions
Topic is locked.
Page: << 1 >>
Function pointers to non static member functions
Author Message
Post #195885 Function pointers to non static member functions

Heres a compile-able model of the code,it doesn't need any eoserv stuff to compile.



#include <map>


class PacketReader {};


class PacketFamily {};


class PacketAction {};


const PacketFamily PACKET_F_INIT;


const PacketAction PACKET_A_INIT;




class game


{


    typedef bool (game::*handler_callback)(PacketReader  reader);


public:


    std::map<std::pair<PacketFamily,PacketAction>,handler_callback>handlers;


    bool default_handler_init (PacketReader reader);


    bool handle(PacketFamily family, PacketAction action, PacketReader &reader);


    void register_default_handlers();


    void register_handler(PacketFamily family, PacketAction action, handler_callback callback);


};




void game::register_default_handlers()


{


    register_handler(PACKET_F_INIT, PACKET_A_INIT,&game::default_handler_init);


}




void game::register_handler(PacketFamily family, PacketAction action, handler_callback callback)


{


    handlers.insert(std::make_pair(std::make_pair(family, action), callback));


}


bool game::handle(PacketFamily family, PacketAction action, PacketReader &reader)


{


for (std::multimap<std::pair<PacketFamily, PacketAction>, handler_callback>::iterator it = handlers.begin(); it != handlers.end(); ++it)


{




 (it->second)(reader); //problem is here




}




return false;


}


-----

The error i'm getting is


 C:\testa\game.cpp|198|error: must use '.*' or '->*' to call pointer-to-member function in 'it.std::_Rb_tree_iterator<_Tp>::operator-><std::pair<const std::pair<PacketFamily, PacketAction>, bool (game::*)(PacketReader)> >()->std::pair<const std::pair<PacketFamily, PacketAction>, bool (game::*)(PacketReader)>::second (...)', e.g. '(... ->* it.std::_Rb_tree_iterator<_Tp>::operator-><std::pair<const std::pair<PacketFamily, PacketAction>, bool (game::*)(PacketReader)> >()->std::pair<const std::pair<PacketFamily, PacketAction>, bool |


I think it's some precedence error but i can't figure it out.


---
Remember when is not an organization nor a fucking group , it's simply an idea that we believe in
and live for.
The priority of Remember when should come before oxygen , as oxygen is cosmetic even life itself is
cosmetic,that's why offer our worthless lives to The "Remember when"
9 years, 38 weeks ago
Post #195886 Re: Function pointers to non static member functions

Try changing that line to:

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

Should at least compile, not sure if that's the desired behavior though. Calling a pointer to a member function requires an object of that type, so I just threw in the `this` pointer to satisfy the compiler so it knows what type the object is that the function pointer belongs to. This is the source I looked at.

---
class EOSERV {
Programmer | Oldbie
Open source EO Client: https://github.com/ethanmoffat/EndlessClient
};
9 years, 38 weeks ago
Post #195888 Re: Function pointers to non static member functions

it worked ,thanks.

---
Remember when is not an organization nor a fucking group , it's simply an idea that we believe in
and live for.
The priority of Remember when should come before oxygen , as oxygen is cosmetic even life itself is
cosmetic,that's why offer our worthless lives to The "Remember when"
9 years, 38 weeks ago
Page: << 1 >>
Topic is locked.
EOSERV Forum > Programming > Function pointers to non static member functions