Skip to main content
GameDev.net gamedev.net
🔒 Locked

Impossible pointer!

Started by uncutno Apr 11, 2002 at 2:04 PM 4 replies 200+ views
Original Post
uncutno
uncutno
Is it possible to, link a non static func inside a class? I know its posible to link functions, but is it posible to link a function thats inside a spesific class? class CExample{ public: long a_value; void AFunc(void){ a_value++: } }; CExample ex; a_pointer = &ex->AFunc(); ???? If not, is it possible to link class static funcs? im trying to create a system, where the different parts are totaly independent, and where i create them as classes, and link the functions into extern func-lists... whitout needing to rewrite the func-list for ewery new func! Is it posible? The goal is to do this: class CExample{ public: CExample(){ RegisterFunc("ExampleAFunc",AFunc); }; void AFunc(void){ }; }; then later: RunFunc("ExampleAFunc"); I repeat myselfe: Is this posible?
-Anders-Oredsson-Norway-
IndirectX
IndirectX
Sure. Use something like this:

void (CExample::*MyPointer)() = &CExample::AFunc;

or alternatively,

typedef void (CExample::*MemberFuncPtr) ();
MemberFuncPtr MyPointer = &CExample::AFunc;

Edit: call functions by this pointer like this:

CExample ClassObject;
CExample *ObjectPointer = &ClassObject

ClassObject.*MyPointer();
ObjectPointer->*MyPointer();


[edited by - IndirectX on April 11, 2002 3:21:57 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
cgoat
cgoat
You could do something like this instead:


class A
{
static MemberFunc(void* ptr);
} object;

RegisterFunc("ExampleFunc", A::MemberFunc, (void*) object);


Then inside the MemberFunc:


A::MemberFunc(void* ptr)
{
A* pobj = (A*) ptr;

pobj->OtherMethod();
pobj->m_value = x;
// etc.
}


This way you can still call the member function on a specific object. I think that's ultimately what you want to do, judging by your example.

Then when you call RunFunc("ExampleFunc") it will do the equivalent of this:


MemberFunc(&object);


(Fixed code)

[edited by - cgoat on April 11, 2002 3:26:40 PM]
uncutno
uncutno
Oh man! :-) Thanks to both of you....

cgoat... Exactly! WOW ,
did not think about that.. But its obvious that when i want to register the func to a spesific class valiable, i allso have the spesific class avilable! :-) This makes alot of sence!
-Anders-Oredsson-Norway-
EvilCrap
EvilCrap
what on earth are you trying to say? why do you say link? you dont link stuff, the compiler does...

do you want pointers to members? pointers to static members?


  

class A
{
public:
static void sfuncA(){}//tatic member function

void (*psfunc)();//pointer to static func


void funcB(){}//member function

void (A::*pmfunc)(); // pointer to member


private:

};//A



void main()
{
A a;
a.pmfunc = a.funcB;
a.psfunc = a.sfuncA;
}
uncutno
uncutno
What do you do with a pointer? do you point it?
What do you do with a func-pointer? do you point it?

I want to have a func-pointer so that i can create a link betveen my gameCORE and my gameOTHERPART
instead of writing " create a link " i write " link " as a verb!

I hope things got clearer ;-)

[edited by - uncutno on April 11, 2002 3:42:55 PM]
-Anders-Oredsson-Norway-

Topic Locked

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

Sign in to reply to this topic.