13 AnswersNew Answer
You haven't seen any because they don't exist. "void" aka "empty / emptiness" refers to no type. This is why we use void as a return type if there is nothing to return. You probably wonder why void pointers are allowed, then. First back to the basics: Pointers are a variable that holds a memory address where another variable of a specific type is. An example: an int pointer is a variable which holds the memory address of an int. A void pointer is a variable that points to a memory address without a type. As void pointers lack the interpretation of what the memory at the stored address is, they can't easily be used as int pointers and others. This is why the compiler forces you to cast a void pointer into a typed pointer before you can work with it more easily.
That website is god of knowledge. ..... thanks..
I already did but maybe I presented it in a hard to understand way. void* is just pointing to a memory address. Fine, no problem. If you want to use it, you need to give the compiler an interpretation what kind of variable is at that memory address. Doing so involves knowledge about the actual type that is stored at the memory address. If you use void* as a parameter this information is "lost" when passing to the function and then is hard-coded into a reinterpret_cast in the function. This is severely error-prone. The void* might point to a double. The implementation of the function reinterpret_casts it to a uint32_t. This is a misinterpretation of the data (very likely resulting in garbage data). A misinterpretation might be the least of the problems you get. Apart from overwriting other data, segmentation faults and memory access violations are just around the corner... This might happen many times in the continuous software development process we have today and might be hard to find (only through proper code review or testing and even testing might not reveal some of the problems). By using templates the type to which the pointer points is not lost. The same is true for Boost.Any (that is also using templates but might be a bit more convenient). Therefore the compiler will either give you warnings or errors, if you try to convert or use the data in a wrong way. I hope this helped.
thanks stefan....I understand now
but I tried and the turbo c++ makes and compiles a void variable.... (to my surprise)
You can still cast a variable to void which will tell the compiler that you don't use this variable (it's a best practice for unused parameters of the "main" function). Anyways, there are multiple options as to why one might think that. Maybe it's a misunderstanding of the code or your compiler allows for more than others. See http://stackoverflow.com/questions/25853258/why-cant-we-declare-a-variable-of-type-void Stackoverflow is an awesome resource :-)
1. You can use them as function parameters 2. You can use void *to point any type
As my initial answer may have understated what you can actually do with void*, I want to incorporate NovaRate0315s answer. As I already stated in my initial answer, void* can be used to point to an arbitrary memory address. This can be used to pass a memory address to other functions which then have to cast this void* into a type. Nevertheless this is *very* insecure and can be considered a bad practice (always reinterpret_cast involved). If you want to work with a pointer of flexible type, pls use templates or the "any" class in the Boost library (look for Boost.Any). Templates and Boost.Any enable you to keep the type and therefore still have the type checks of the compiler. This will make your program less error-prone.
HEY!!! do you know what is a handle??? well all microsoft is using handles in windows a window application is not possible without using atleast one handle. I dont think so that working with void* is a bad practice
what about DirextX?
Again, no example helps the case. Just because DirectX uses it does not mean it's an acceptable practice. This is not about how others do it, this is about how *should* I or everyone do it. If you can point out weaknesses in my argumentation, pls do so. I'm eager to learn.
I edited my question please answer
Mukul, I know what a handle is. Also, your answer is a bit Microsoft specific. Furthermore, the API you refer to is a C API (as all operating system API that I know) and not a C++ API. Also, you did not argue my actual point that this is error-prone and in C++ there are better ways to do the same thing. In every language you should leverage the means the language gives you to write code that is less error-prone / safe and readable. Just because something exists doesn't mean it's a good idea to use it. If you can't avoid it, you have to but in any other way avoid this.