c++void-pointers

What is a void pointer in C++?


Possible Duplicate:
What is a void pointer and what is a null pointer?

I often see code which resembles something like the following:

void * foo(int bar);

What does this mean? Does it mean that it can return anything? Is this similar to dynamic or object in C#?


Solution

  • A void* does not mean anything. It is a pointer, but the type that it points to is not known.

    It's not that it can return "anything". A function that returns a void* generally is doing one of the following:

    This construct is nothing like dynamic or object in C#. Those tools actually know what the original type is; void* does not. This makes it far more dangerous than any of those, because it is very easy to get it wrong, and there's no way to ask if a particular usage is the right one.

    And on a personal note, if you see code that uses void*'s "often", you should rethink what code you're looking at. void* usage, especially in C++, should be rare, used primary for dealing in raw memory.