Skip to main content
gamedev.net
🔒 Locked

Unknown class Pointer-to-Method

Started by SherlockG Jan 13, 2003 at 4:26 PM 4 replies 200+ views
Original Post
SherlockG
SherlockG
Hi! I''m trying to program a registering process in which any class instance can send one of it''s method to a "call manager". So, I need to point to a method of an object. I know how I can do this for a known object type, by receiving (TMyClass::*fpFunc)... but I don''t know how to do this if I don''t know this type. I''ve read I could receive the object AND the function pointer (the adress and the offset), but I can''t make this work. Below is a code that is working for global or static __cdecl functions. //********************************************** class A; class B; class C; class A { public: A() {} ~A() {} void SomeFunction( char* SomeParam ); }; class B { public: B() {} ~B() {} void SomeOtherFunction( char* SomeParam ); }; class C { public: C() {} ~C() {} void SetFunctionPointer( void (*_fpFunc) ( char* SomeParam ) ) { fpFunc = _fpFunc; } private: void (*fpFunc) ( char* SomeParam ); }; C oC; void main( void ) { A oA; B oB; //oC.SetFunctionPointer( oA.SomeFunction ); //oC.SetFunctionPointer( oB.SomeOtherFunction ); } //********************************************** So, briefly, I want to store an unknown object adress and one of it''s method in another object, so I can call it later. Thanks for any help or hints. Let''s program! Sherlock
[Sherlock]
civguy
civguy
How would you call it later? You need to have an object on which to call it after all, and since it can be a function pointer to any object's function, what object to use? If you just want to bind it to some specific object, boost::bind with boost::functionmight be what you need. Well, it's probably what you need no matter what requirements you have

Example copied from the bind page:

    
class button
{
public:

boost::function<void> onClick;
};

class player
{
public:

void play();
void stop();
};

button playButton, stopButton;
player thePlayer;

void connect()
{
playButton.onClick = boost::bind(&player::play, &thePlayer);
stopButton.onClick = boost::bind(&player::stop, &thePlayer);
}


[edited by - civguy on January 13, 2003 5:46:39 PM]
Fruny
Fruny
quote:

I''ve read I could receive the object AND the function pointer (the adress and the offset), but I can''t make this work.



Where ?


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
SherlockG
SherlockG
The Boost Library worked perfectly!
Thank you very much for your help!
Here is what I wanted exactly:

#include <boost/bind.hpp>
#include <boost/function.hpp>

class A
{
public:
A() {}
~A() {}

void SomeFunction() { printf( "A::SomeFunction\n" ); };
};

class B
{
// [...]
};

class C
{
public:
C() {}
~C() {}

void SetFunctionPointer( boost::function _fpFunc )
{
fpFunc = _fpFunc;
}

boost::function fpFunc;
};

A oA;
C oC;

void main( void )
{
oC.SetFunctionPointer( boost::bind(&A::SomeFunction, &oA) );
[...]
oC.whenclickedon();
}
[Sherlock]
SherlockG
SherlockG
I know, but the point was to be able to call from ANY class. And I don''t see every and each class of the code being inherited from a totally unuseful class.. using boost::function and boost::bind answered my problem. This is not as fast as two pointers (there are three with this) but this is not casted on each frame, so I can handle the drawbacks.

Thanks anyway!

Code, compile, pray, works! Code, compile, pray...
[Sherlock]

Topic Locked

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

Sign in to reply to this topic.