How to get rid of void-pointers.

Posted by Patrick on Stack Overflow See other posts from Stack Overflow or by Patrick
Published on 2010-06-11T20:55:38Z Indexed on 2010/06/11 21:02 UTC
Read the original article Hit count: 355

Filed under:
|
|
|

I inherited a big application that was originally written in C (but in the mean time a lot of C++ was also added to it). Because of historical reasons, the application contains a lot of void-pointers. Before you start to choke, let me explain why this was done.

The application contains many different data structures, but they are stored in 'generic' containers. Nowadays I would use templated STL containers for it, or I would give all data structures a common base class, so that the container can store pointers to the base class, but in the [good?] old C days, the only solution was to cast the struct-pointer to a void-pointer.

Additionally, there is a lot of code that works on these void-pointers, and uses very strange C constructions to emulate polymorphism in C.

I am now reworking the application, and trying to get rid of the void-pointers. Adding a common base-class to all the data structures isn't that hard (few days of work), but the problem is that the code is full of constructions like shown below.

This is an example of how data is stored:

void storeData (int datatype, void *data);    // function prototype
...
Customer *myCustomer = ...;
storeData (TYPE_CUSTOMER, myCustomer);

This is an example of how data is fetched again:

Customer *myCustomer = (Customer *) fetchData (int datatype, char *key);

I actually want to replace all the void-pointers with some smart-pointer (reference-counted), but I can't find a trick to automate (or at least) help me to get rid of all the casts to and from void-pointers.

Any tips on how to find, replace, or interact in any possible way with these conversions?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c