C++: String and unions
- by sub
I'm having a (design) problem:
I'm building an interpreter and I need some storage for variables. There are basically two types of content a variable here can have: string or int.
I'm using a simple class for the variables, all variables are then stored in a vector.
However, as a variable can hold a number or a string, I don't want C++ to allocate both and consume memory for no reason.
That's why I wanted to use unions:
union
{
string StringValue;
int IntValue;
}
However, strings don't work with unions.
Is there any workaround so no memory gets eaten for no reason?