In C ++, I found the most convincing use case for void * pointers to provide code for storing arbitrary "user data" on an object that they already use.
Let's say you wrote a class representing Car for use in software that does useful things with Car objects (simulating traffic, car rental inventory, whatever). Now say that you are in a situation where your application wants to track arbitrary contents of a Car trunk. Details of what is stored in the trunk are not important for the Car class and can be any - it really depends on the purpose of the application using the Car class. Enter the void * pointer.
class Car { public:
Now, any application using the Car class has the ability to attach an arbitrary data object to an existing Car object so that it can be obtained from any code that has a Car object. The contents of the body "move" with the Car object, wherever it is in your code.
In this case, there are two uses for void *.
The first is your class template, based on the content type of the contents of the trunk:
template <class TrunkContentsType> class Car { public:
It seems overly invasive. The type of trunk contents is important only for the application. Algorithms and data structures that work with Car objects do not care about what's in the trunk. By clabbling a class, you force applications, using the class, to choose the type of trunk content, but in many cases, applications also do not care about the contents of the trunk.
The second alternative is to derive a new class from Car, which adds a data member and accessors for the contents of the trunks:
class Car { public:
The new CarWithTrunkContents class is an application-specific class that adds a data member of the type in which the application must store the contents of the trunk on the car. It also seems unnecessarily heavy. Why should you get a whole new class to add an extra piece of data that doesn't affect class behavior? And if it is common enough for applications using the Car class to store the contents of trunks, why make each application output a new class for its specific type of trunk content?
Finally, although my far-fetched example of the contents of a connector line can paint a vivid picture of arbitrary contents of a connector line moving with a Car object, in practice, you are likely to provide an even more general mechanism for linking application-specific data to a Car :
class Car { public:
Thus, the application can attach an object representing the contents of the trunk, or an object representing the license and registration of the driver, or an object representing the lease, or something else. I saw a void * pointer called userData (that is, understood by the user of the class), "blindData" (ie the class is blind to the contents of the object that it carries), or "applicationData" (i.e. data of type and goals defined by the application).