• Advertisement

Archived

This topic is now archived and is closed to further replies.

Class Function Pointers

This topic is 5216 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, say i have a class: //Example class CObject { public: //Some random function void RunMe(bool bRun); }; Say i have a class in memory, and i want to grab a function pointer to the function it contains: typedef void (*RUNMEPROC)(bool); void Foo() { RUNMEPROC rm; CObject * pObject = new CObject; rm = (RUNMEPROC)pObject->Runme; //HOW CAN I DO THIS? }; My compiler keeps giving me an error: couldn''t convert '' '' to type ... How can i overcome this? THanX in advance, John Beaton

Share this post


Link to post
Share on other sites
Advertisement
OH... and one more thing...

i know the type of the function and its address but i do not know the base class it originates from ... yuk.

so i am using:

LPINITPROC p = (LPINITPROC)GetFunction("InitGL");

In this code, GetFunction returns a pointer to the global GL class''s InitGL function.


CheerZ,
JB

Share this post


Link to post
Share on other sites
Hi beaton

This is where you get into syntax that looks like it should be some kind of chat client emoticon.


class example
{
public:
void memberfunction();
};

//member function pointer

void (example::*pmemberfunction)() = &example::memberfunction;

class derived : public example
{
public:
...
};

void (derived::*pderivedmfunc)() = &derived::memberfunction;



You call the function pointers a little differently too.



(derivedinstance.*pderivedmfunc)();
or
(pderivedinstance->*pderivedmfunc)(); // if pderivedinstance is a pointer to a derived instance






[edited by - Jingo on November 14, 2003 7:11:21 PM]

Share this post


Link to post
Share on other sites
Cheerz, but i want to do it the OTHER way....


I have a global repository of objects, all derived from one base CObject class. I can retrieve these objects by their names:

eg.
CObject * pObject = CRepository::Instance()->Get("OpenGL");


I want some way of retrieving a function pointer from this:

eg.
LPINITPROC proc = pObject->GetFunction("Initialize");


I DO NOT know what class it will be, only that it is derived from CObject. It could be CDirectX, COpenGL, anything, really.

... and i also need some way of calling it.

THankz very much
John


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sounds like a job for runtime polymorphism

Share this post


Link to post
Share on other sites
its okay
just fixed it with an ULTRA-cheap hack using memcpy().... there is probably a better way but this is fast.... just ugly

Share this post


Link to post
Share on other sites

  • Advertisement