Jump to content
  • Advertisement

Archived

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

Nacho

Using void pointers

This topic is 5803 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´ve got a very simple question about void pointers: Where can you apply them in your programs? I´ve been reading the MSDN and the only information provided is that they can point to any kind of variable and that they can only be dereferenced if they are casted to another type. I tought that they may be applied to a situation like the following one (altough I consider that this example is pretty useless): const int INT = 1; const int FLOAT = 2; const int DOUBLE = 3; void Function(void *ptvoid,int type) { switch(type) { case INT: { *(int*)ptvoid = 5; break; } case FLOAT: { *(float*)ptvoid = 10.5f; break; } } return; } int main() { int vari = 0; float varf = 0.0f; Function(&varf,FLOAT); Function(&vari,INT); return 1; } Is that correct or did I completely misunderstand the purpose of this kind of pointers? All replies are welcome, thanks!

Share this post


Link to post
Share on other sites
Advertisement
Does it compile? It looks like it will. And yes that shows one use for them. void pointers are more of a C thing than a C++ thing. If you dig through the windows headers, you''ll find that many of the handle types are really just void pointers - hwnd, hinstance, handle etc.

Share this post


Link to post
Share on other sites
Well I would think it would be usefull for a function in a game engine... like you could have an AI object that is given another object via the void pointer and then the function would find the class type and take appropiate action...

Share this post


Link to post
Share on other sites
The only reason I declare void pointers is to cast them later on. Say you have two classes that are dependent on each other *(pointers to the other type in the class definition). One class has to be defined first, but it can''t have a pointer to the second class since that hasn''t been defined yet. So in the first class I use void pointers and cast them as needed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well if you hit that problem you can just prototype the class

class A;
class B;

class A
{
public:
B tmpB;
};

Share this post


Link to post
Share on other sites
quote:
Original post by Psybr
The only reason I declare void pointers is to cast them later on. Say you have two classes that are dependent on each other *(pointers to the other type in the class definition). One class has to be defined first, but it can''t have a pointer to the second class since that hasn''t been defined yet. So in the first class I use void pointers and cast them as needed.

Use a forward reference instead:

  
class Foo;

class Bar
{
public:
//...

private:
Foo* fooPtr;
};

void pointers should generally be avoided in C++. The above AP''s example won''t work, since he''s creating an actual instance of the other class. Forward referencing only works with pointers.




"The churches used to win their arguments against atheism, agnosticism, and other burning issues by burning the ismists, which is fine proof that there is a devil but hardly evidence that there is a God."
Ben Lindsey and Wainwright Evans

Share this post


Link to post
Share on other sites
quote:
Original post by Ignacio Liverotti
Hi! I´ve got a very simple question about void pointers: Where can you apply them in your programs?

Legacy interoperability.

void pointers were a method for type agnosticism in C, and since C++ is largely a superset of C it retains that functionality. C++ imposes the restriction that casts from void pointer to any other pointer type must be explicit, but other than that they function very much like in C. C++, however, provides superior mechanisms for type-agnostic programming (templates) and run-time type information as well (typeid), making the use of void pointers in a modern C++ design unnecessary and undesirable.

Basically, the only need for void pointers is to interface with older C and (bad) C++ code.

Incidentally, your example is better solved with overloading. C++ tries to emphasize type safety, and code like that circumvents it at no advantage.

Share this post


Link to post
Share on other sites
Wow! Too many replies! Thanks to all of you people! And yes, the code compiles, but I just wanted to know where can void pointers be applied. Thanks Oluseyi for your reply. You´re right, an overloaded Function() would be a better solution, but can I apply void pointers to any specific situation? Thanks!

Share this post


Link to post
Share on other sites
I''m no network programming expert, but if I remember correctly, DirectPlay requires you to cast the data sent to a (void*) pointer. Even with all the OO programming in the world, you can''t avoid losing type info when going over a network, so you have to encode it somehow.

Cédric

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void pointers are not needed. Use function overloading, templates, or inheritance. Basically void* is a thing to use with casting, and casting is a thing to use when your language does not have the features you need.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!