Skip to main content
gamedev.net
🔒 Locked

Functions Pointers

Started by kroiz Oct 13, 2000 at 1:28 AM 6 replies 200+ views
Original Post
kroiz
kroiz
Hi Friends ;-> I''m having a little probleme with functions pointers. who do i make a FP that can point to 2 differant member function of 2 differant classes? class A{ public: void DoA(void); }; class B{ public: void DoB(void); }; I wanna make my FP point on both DoA() and DoB(). ho and if i later derive from one of that classes can i make this FP point on one of the derived class member function? help will be eppreciated. or may be a tutrial reference.
0 error(s), 0 warning(s)
LilBudyWizer
LilBudyWizer
You really don''t want to use function pointers to member functions unless they are static. C++ inserts an extra parameter. The "this" pointer is a parameter into the function though you never declare it. A static member doesn''t have a "this" pointer so you can use it like a regular C function. If you want a function pointer to point to two functions with the same parameters you just declare it and assign it. If they have differant parameters then you have to use a union.

You should really try using a common base class with a virtual function assuming they have the same parameters. Then you just declare a pointer to the base class and assign it a value of one of the two derived classes. After that you just call the virtual function in the base class and it will actually execute the function in the derived class. What you proposed doing is completely out of line with the idea of object oriented programming. That idea is that the data and the functions that operate on it are inseperable. If you were using C it would be how you keep it object oriented, but C++ provides specific support.
Keys to success: Ability, ambition and opportunity.
Tornado
Tornado
Yeah, LilBudyWizer''s right, you should use virtual functions in this case.
Function pointers will be harder to setup with member functions.
There was a thread a while ago about making a function pointer point at member functions, in this forum, search for it.



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
AlekM
AlekM
I found a really good way to use function pointers for a class method.

I am developing a messaging system, similar to MFC''s message handler.

With an ON_COMMAND event, you need a way to store the method pointer and this can not be a static method. So, you create a function pointer.

If you want to create an FP for a class method, this is how you do it.

    
typedef void ( CDXComponent::*DXMessageMethodPtr)();

DXMessageMethodPtr pDXMessageMethod;

pDXMessageMethod = CDXComponent::OnUserDefinedCommand;



kroiz
kroiz
The idea to use FP rose during a previos post of mine
in the subject of the winmain loop, in general i didn''t want
to be conformed to that one loop, i wanted to be in others loop
like menu loops, but there is a need to return to the winmain
loop in order to parse the messages.

0 error(s), 0 warning(s)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.