I think I am in a rather big pickle. I am trying to reengineer some old C code into C++ so I can utilize the capabilities of the STL, Boost, OO, and so forth. There is a section in the C code where they are creating a message and payload to be sent across a network. Essentially, they created their own List, Vector, and Hash capabilities. I would like to convert all these to STL objects, but I'm having problems figuring out how to remedy something. They create objects of various types, raw chars, null terminated chars, int's, int array's, doubles, double arrays. Then they take these objects and put them into the hash. So I'm trying to figure out how get a hash to accept multiple types. I was trying to figure out if I could use an STL map, but I still have to put in a maximum of one class type as the value. I tried to create a union of the objects, but the standard doesn't allow for that.
The closest I've gotten so far is to create classes of each of those data types that are being used, then a parent class (payload) that contains a pointer to each of the data types. Are there any other suggestions?
Although a hash_map isn't part of the standard library, many C++ implementations do provide one, albeit in a different namespace. What compiler are you using?
I think you could use pointer-to-void (i.e. void*) as the type.. This should allow you to put any pointer type in the map, so you can have a pointer to your ints, chars, doubles, etc.
The closest I've gotten so far is to create classes of each of those data types that are being used, then a parent class (payload) that contains a pointer to each of the data types. Are there any other suggestions?
Standard C++ introduce templates. Take a look to see how it solve above problem faced commonly in most old C programs. vector<int> vector<float> vector<MyStruct> vector <MyClass> etc etc provide a clue on how it handle.
kbw...Right now I'm running on MSVS2005, but I'm going to be going to 2010 before long. I'll look into a hash_map. The void* might be a little above my head, but I'll look into that too. Thanks for the suggestions.