🔒 Locked
member function pointers on C++
Advertisement
Original Post
Hi.
I''ve been trying to use a function pointer into a C++ class (pointing to member functions from that class), but I can''t make it work...
the only way I''ve found to make it work is declareting the member functions wich I''ll be pointing to as "static":
#include <iostream>
class player_t{
public:
player_t();
void (*exec)( void );
static void my_function( );
};
player_t:
layer_t(){
exec = my_function;
}
void player_t::my_function( ){
cout << "hi, I''m my_function();" << endl;
}
int main( void ){
jugador_t J;
J.exec();
return 0;
}
But doing so those functions will lack of the implicit "this" pointer passed in all member functions. So I''ll have to pass them on an explicit way:
#include <iostream>
class player_t{
public:
player_t();
void (*exec)( player_t * );
static void my_function( player_t * );
};
player_t:
layer_t(){
exec = my_function;
}
void player_t::my_function( player_t *THIS ){
cout << "hi, I''m my_function();" << endl;
}
int main( void ){
jugador_t J;
J.exec( &J );
return 0;
}
How ever, I''m trying to find a way to use function pointers without having to declare them as "static", so I can keep my code clean without any wierd parameters...
ANY IDEA??????????????
TNX
jakovo
Nope, you've nailed the problem : it is not possible in standard C++. (Have a nice day)
And it is even worse with pointer to virtual member functions ... which are not necessarily pointers (but offsets into the virtual function table, which in its turn holds pointers to member functions).
You can use pointers to member function, but have to use a different syntax, binding it to the object at the call point (and not before.)
void (player_t::*exec)(void);
...
exec = my_member_function
...
player_t player;
player_t *p_player = &player
...
player.*exec();
p_player->*exec();
Note: There is nothing you can do with the expressions player.*exec and p_player->*exec beside calling them on the spot.
Some compilers (borland's, g++) will let you do the cast from bound-pointer to member function (the expression obj.*pmf or pobj->*pmf) to regular function pointer (since the object is known, so is the type and vtbl base address in the case of a virtual function.)
As an aside to those people who think that a member function is just a function with the this pointer as first parameter, I say "Not necessarily: there are no constraints as to how the compiler implements them."
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on October 7, 2002 1:05:14 AM]
And it is even worse with pointer to virtual member functions ... which are not necessarily pointers (but offsets into the virtual function table, which in its turn holds pointers to member functions).
You can use pointers to member function, but have to use a different syntax, binding it to the object at the call point (and not before.)
void (player_t::*exec)(void);
...
exec = my_member_function
...
player_t player;
player_t *p_player = &player
...
player.*exec();
p_player->*exec();
Note: There is nothing you can do with the expressions player.*exec and p_player->*exec beside calling them on the spot.
Some compilers (borland's, g++) will let you do the cast from bound-pointer to member function (the expression obj.*pmf or pobj->*pmf) to regular function pointer (since the object is known, so is the type and vtbl base address in the case of a virtual function.)
As an aside to those people who think that a member function is just a function with the this pointer as first parameter, I say "Not necessarily: there are no constraints as to how the compiler implements them."
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on October 7, 2002 1:05:14 AM]
Time to develop a GDNet General C++ FAQ. This question was asked only days ago (it''s probably on page 2 of this forum)...
Actually, here''s one on the same page: Function Pointers to Member Functions, and here''s another: Calling a member function from inside a static member function (page 2).
Actually, here''s one on the same page: Function Pointers to Member Functions, and here''s another: Calling a member function from inside a static member function (page 2).
quote:
Original post by Oluseyi
Time to develop a GDNet General C++ FAQ. This question was asked only days ago (it's probably on page 2 of this forum)...
A Wiki might be better (if you keep the crap posters at bay)
Edit: Hey, look at this everything2 entry.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on October 7, 2002 1:44:17 AM]
quote:
Original post by Fruny
A Wiki might be better (if you keep the crap posters at bay)
Excellent idea! Perhaps members could be given free reign to modify the Wiki, creating branches by so doing, which are then later grafted back into the main tree. Sort of like CVS-meets-BBS. That would allow us to maintain a consistent level of grammar, etc, and easily discard spurious posts. Time to go look at those WikiWikiClones.
quote:
the GDNet search is just a frontend to Google using site:gamedev.net as a search argument.
AFAIK, Google webspiders are not allowed into the forums, and Google won''t index it. The site:gamedev.net will search the whole site, but the forums.
The GDNet search feature was directly connected to the forum DB (and that''s why it was disabled, because some people used it as a platform to hack GDNet).
/ Yann
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement