C++ void** parse
Hi all, i want to use a function like this:
function(void** thing);
The goal is to pass any object i want:
Ai* obj;
obj = new obj[10];
function((VOID**)&obj);
At this step all seems right, but in the function itself:
function(void** thing)
{
thing[0].method();
}
Here i dont know what to do, the compiler says:
expression must have class type
How i need to change this to work, i dont find how.
I hope you can help me , Thanks!
In C++ we avoid the void type. That is because it goes against the principle of type safety.
By casting a pointer of type MyObj* to void*, you lose all type information. The compiler is telling you that objects of type void do not define a function called "method()".
You want to use generics to solve this problem:
By casting a pointer of type MyObj* to void*, you lose all type information. The compiler is telling you that objects of type void do not define a function called "method()".
You want to use generics to solve this problem:
template<typename T>void foo(T t){ t.method();}
Not necessarily; depending on the situation, virtual functions may be more suited to the task. But fpsgamer is right: void* is not to be used in C++ unless you know what you're doing. _Camus_, what precisely are you trying to accomplish?
Thanks, the main problem is this:
class one
{
... //stuff
function(two obj);
};
class two
{
...//stuff
other_function(one obj);
};
if i include one i need to include the other, but in that case, both are
included xD and i cannot do that, thats why i try using void** to pass
whatever i want, but it seems more difficult than i thought.
The problem its that class one its the physx, and the class two its the
Ai so, i need to use physx to my Ai, but i need to calcule things of Ai
on physx, there its a way to deal ?
Thanks again
class one
{
... //stuff
function(two obj);
};
class two
{
...//stuff
other_function(one obj);
};
if i include one i need to include the other, but in that case, both are
included xD and i cannot do that, thats why i try using void** to pass
whatever i want, but it seems more difficult than i thought.
The problem its that class one its the physx, and the class two its the
Ai so, i need to use physx to my Ai, but i need to calcule things of Ai
on physx, there its a way to deal ?
Thanks again
Forward declarations:
class two;class one{... function(two &obj);};class two{... other_function(one &obj);}
For future reference, this is why you should always explain what you are trying to achieve, instead of just what you are doing.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement