Sorry in advance for the lengthy explanation!
I have a C++ application that uses a hash_map
to store a tree of information that was parsed from a text file. The values in the map are either a child hash_map
or a string
. These values were parsed from a text file and then stored into the map.
I wanted to avoid having to send the strings and maps as a copy to the hash map assignment function, so when file was parsed, I created a pointer to new string()
or a new hash_map()
and stored the value into the map as "arbitrary" data (pointer to a void).
However, this poses a pretty big problem when it comes to clean-up, as deleting a void doesn't behave like one would want it to (and it makes sense). I looked for an easy solution by just creating an Object
class and made child classes called StringObj
and HashMap
, which stored their respective data, and the appropriate destructor was called since the hash_map
value type was changed to a pointer to an Object
.
Is there an easier way to solve this? I looked into dynamic casting and thought it might work well, since I could catch the exception from the failed cast, and treat it appropriately, but I can't help but feel there might be a simpler solution, or that I'm over-complicating it a bit.
Suggestions?
Thanks in advance, Jengerer