Skip to main content
gamedev.net
🔒 Locked

pointer to method

Started by siddim May 20, 2003 at 1:34 PM 8 replies 1.4k views
Original Post
siddim
siddim
hi, i am having hard time trying to make my pointer to method work. what i want is a generic pointer to a method, but a method of any class. is that possible? if yes, how?
civguy
civguy
www.function-pointer.org
kuphryn
kuphryn
Post whatever code you have right now. What is the error?

Kuphryn
siddim
siddim
i have use that site, but i still need something more specific.
in fact, i don''t have a compiling error, i want to do something i am not sure it''s ok,

let say i have a pointer

void *mth(void);

i would like to have this pointer point on any method of any class that is void mthName(void); can i do it or i have to absolutly define for what class my pointer is?
ex:

void MyClass::*mth(void);

Way Walker
Way Walker
quote:
Original post by siddim
i have use that site, but i still need something more specific.
in fact, i don''t have a compiling error, i want to do something i am not sure it''s ok,

let say i have a pointer

void *mth(void);

i would like to have this pointer point on any method of any class that is void mthName(void); can i do it or i have to absolutly define for what class my pointer is?
ex:

void MyClass::*mth(void);




First,

void *mth(void);

is a prototype for a function taking no arguments and returning a void pointer. I think you want

void (*mth)(void);

Which is a pointer to a function taking no arguments and returning nothing.

And I don''t think you can do what you want to do. I''m fairly certain that the compiler needs to know the innerworkings of your class. If you don''t care about portability, you could just declare a void pointer

void *mth;

and then cast the function on assignment and invocation. Would probably require templates or macros or something, which may be how you should be doing it anyway (Though I can''t say for sure because I don''t know what problem you''re trying to solve).
jenova
jenova
this is what vtable''s are for....

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
cgoat
cgoat
This might be what the previous poster meant about vtable, but you can make an interface class like so:

  
class VoidFunctionClass
{
virtual ~VoidFunctionClass() {}
virtual void VoidFunc(void) = 0;
};

class ClassOne : public VoidFunctionClass
{
virtual void VoidFunc(void)
{
}
};

class ClassTwo : public VoidFunctionClass
{
virtual void VoidFunc(void)
{
}
};

void SomeFunction(VoidFunctionClass *pClass)
{
pClass->VoidFunc();
}

// somewhere in main

ClassOne one;
ClassTwo two;
SomeFunction(&one);
SomeFunction(&two);


You get the drift.
siddim
siddim
ok, so my first plan did''n work, now my alternative one did''n compile, can you see the mistake?

i define a struct:

struct sTask
{
bstring szTaskName;
bstring szTaskClassOwner;
int nType;
int nOccurance;
int nMSinterval;
int nLastCall;
int nTaskID;
int (ITask::*mth)(int);
};

now some of my class implement the interface ITask :
class ITask
{
public:
virtual void addPOSTNEWtask(std::vector vTask_)=0;
virtual void addPREDELETEtask(std::vector vTask_)=0;
virtual int taskRedirector(int nTaskID_)=0;
};

now my (CTieFighter: public ITask) class want to add a task:
void CTieFighter::addPOSTNEWtask(std::vector& vTask_)
{
sTask hop;
hop.szTaskName="TieFighter initialization";
hop.szTaskClassOwner="CTieFighter";
hop.nType = TASK_OCCURANCE_NO_DEADLINE
hop.nOccurance=1;
hop.nMSinterval=0;
hop.nLastCall=0;
hop.nTaskID = CGlobalInfo::Instance()->getUniqueID();
hop.mth = &ITask::taskRedirector;

vTask_.push_back(hop);
}

until here everything is fine, but in my CTaskManager when i try to call the method :
nRet= vTransitionTask.mth(nTaskID);

i got this compiler error:
error C2064: term does not evaluate to a function

??

thanks

Siddim








daerid
daerid
wow... why don''t you just use std::mem_fun?

As long as the method takes zero or one argument(s), then it''ll be fine.

Use like this:


  
#include <iostream>
#include <functional>

class A
{
public:
void SomeFunction() {
std::cout << "A::SomeFunction()" << std::endl;
}
};

struct B
{
void AnotherFunction( int i ) {
std::cout << "B::AnotherFunction( " << i << " )" << std::endl;
}
};

int main()
{
std::mem_fun_t< void, A > pfn( A::SomeFunction );
A a;
pfn( &a ); // call SomeFunction on a


std::mem_fun1_t< void, B, int > pfn2( B::AnotherFunction );
B b;
pfn2( &b, 2 ); // call SomeFunction on b with arg i = 2

return 0;
}
daerid@gmail.com

Topic Locked

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

Sign in to reply to this topic.