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

void* problem

Started by siddim Jun 2, 2003 at 11:22 AM 2 replies 1k views
Original Post
siddim
siddim
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
morebeer
morebeer
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?
siddim
siddim
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.
null_vector
null_vector
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]
find / -name "your base" -exec chown -R us:us {} ;

Topic Locked

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

Sign in to reply to this topic.