Archived

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

void* problem

This topic is 5310 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, i am implementing a TaskManager, what i want is that any class that want to define a task will simply send a structure to the TaskManager. this structure will hold information about the task (name, expiration time etc) plus a pointer to the method of the class in question. I know that i can''t have a general method pointer to any method of any class, i have to specify the class, so i was thinking about using a void* pointer. But it did''n work, Is it possible to do such manipulation? or i have to absolutely cast the pointer before calling the method? here''s what my code look like: the structure: struct sTask { int nID; int nType; bstring szName; bstring szOwner; void* pMth; }; the setting of the task informations: // i must do that so it can compile void (CStarGate::*pmth)(void); pmth =&CStarGate::taskBlink; sTask task; task.nID = CGlobalInfo::Instance()->getUniqueID(); task.nType= TASK_MONO_CYCLE; task.pMth = &pmth; task.szName="taskBlink"; task.szOwner="CStarGate"; this->vTask.push_back(task); //this vector is later read by the task manager then in my TaskManager i try to call the method, i try many way but none work. It''s the first time I am dealing with void* pointer and i can''t find descent documentation, do you have any link or idea about this problem? thanks

Share this post


Link to post
Share on other sites
i hope i understood that correctly. anyway, here i go:

you could define a common interface (or in this case, a base class)for every class which wants to implement a task function, like this:


class TaskBase
{
public:

//task manager calls this when it''s time for it.

//just change ''params'' to any type you need..

virtual void OnTask ( void* params ) = 0;

sTask TaskInfo; //your struct

};


you can derive any class you want from this class. then, in the TaskManager you keep a list of pointers to all objects, and it walks through the list and calls the OnTask() method.

this would be more elegant because of the unified interface instead of arbitrary function names etc. of all the different classes.

is that what you want?

Share this post


Link to post
Share on other sites
yea well that was my first implementation, but maybe a class will want to define more then one task, so that''s why it would be nice to have the pointer thing.

Share this post


Link to post
Share on other sites
what you want is

void(variablename*)();

not

void *

two very different things, value pointers and function pointers

EDIT: two not too :-/

[edited by - null_vector on June 2, 2003 1:02:57 PM]

Share this post


Link to post
Share on other sites