Using void pointers

Started by
19 comments, last by Nacho 21 years, 8 months ago
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!
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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...
-=CrAKiN:ShOt=-I could put something witty here but I'm not that stupid...
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.
well if you hit that problem you can just prototype the class

class A;
class B;

class A
{
public:
B tmpB;
};
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
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.
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!
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
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.

This topic is closed to new replies.

Advertisement