🔒 Locked
pointer to method
Advertisement
Original Post
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);
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);
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).
This might be what the previous poster meant about vtable, but you can make an interface class like so:
You get the drift.
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.
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
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
virtual void addPREDELETEtask(std::vector
virtual int taskRedirector(int nTaskID_)=0;
};
now my (CTieFighter: public ITask) class want to add a task:
void CTieFighter::addPOSTNEWtask(std::vector
{
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
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:
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;
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement