I am relatively new to C++, so trying to find the best way to store different types of values under one entity. Context is, I have a function which returns a struct and one of the values can be of different types. At any time, only one value will be set. Best case for me would be if I can do something like this:
Union Union1 {
int x;
char y;
double z;
}
executor(Union1 union1) {
if(union1.x.isSet()) {
doSomethingUsingX();
} else if(union1.y.isSet()) {
doSomethingElseUsingY();
}
}
I dont think this is possible because c++ unions use same memory space. So is there something I can use?