#103379 - Mucca - Thu Sep 21, 2006 2:00 pm
Having taken a look recently at more powerful languages such as Haskell and OCaml, I began to yearn for such generic programming capabilities in C++, and decided to see what I could muster up with templates. So anyway, I have a situation where I need to iterate across an array of numbers, locate an object based on that number, and call a member function passing the located object as a parameter. Seeing as I would probably need roughly the same functionality in a number of classes, I decided to try to factor the code into a templatized static function, taking a template class, the array of numbers, and, crucially a pointer to a member function of the templatized class. Basically what I was trying to achieve was a c++ version of Haskell's map function, albeit with limitations on the function parameter.
I subsequently tried to call the method
Sadly, as you may have guessed by now, it failed to compile, complaining about an error before the comma in the line containing the member function parameter to MapMethodToEntityIDList.
Anyway, Im using, and am stuck using, gcc 2.9.5, and I was just wondering if the problem lies with my compiler, or with my syntax, or with the idea itself? Are pointers to member functions supported at all? Or are they just a myth? Of course I could make it a pointer to a static function, pass my object, cast, and call my member function, but that just doesnt look very nice. Obviously this is a trivial example, its every bit as easy to write the loop everywhere its needed, but its definitely a useful technique, especially if more complicated processing of the idList is required.
Code: |
class EntityFactory{ .. // Map a member function of an object of class TYPE taking an Entity* parameter to all // entity ids in list. Level is used to find entities template<class TYPE> static void MapMethodToEntityIDList ( TYPE * caller, void (TYPE::*Method)(Entity*) method, const ID* idList, Level* level ) { for( int i=0; i<refList->numItems; i++ ) { Entity* e = level->FindEntity( idList[i] ); caller->*method(e); } } }; |
I subsequently tried to call the method
Code: |
SyncEntity::Init() { /// Call AddObject for each id in list EntityFactory::MapMethodToEntityIDList<SyncEntity>( this, SyncEntity::*AddObject, this->idList, this->level ); } |
Sadly, as you may have guessed by now, it failed to compile, complaining about an error before the comma in the line containing the member function parameter to MapMethodToEntityIDList.
Anyway, Im using, and am stuck using, gcc 2.9.5, and I was just wondering if the problem lies with my compiler, or with my syntax, or with the idea itself? Are pointers to member functions supported at all? Or are they just a myth? Of course I could make it a pointer to a static function, pass my object, cast, and call my member function, but that just doesnt look very nice. Obviously this is a trivial example, its every bit as easy to write the loop everywhere its needed, but its definitely a useful technique, especially if more complicated processing of the idList is required.