EDIT: thanks for responses. Adding breaks to the switch; the problem is indeed that I want to be able to use the struct after the switch statement.
I want to be able to do something like the following:
int foo (int type, void* inVar) {
switch(type) {
case 0:
struct struct-type-one mystruct = *( (struct struct-type-one*) inVar);
break;
case 1:
struct struct-type-two mystruct = *( (struct struct-type-two*) inVar);
break;
case 2:
struct struct-type-three mystruct = *( (struct struct-type-three*) inVar);
break;
}
// use mystruct.data
}
Basically, the idea is that inVar
is a pointer to a struct, and type
tells us what type we need. The structs all have data, but the type of the struct determines the type of the data. I want to do this because in the code after the switch, we can then do operations on mystruct.data
without having to worry about what type the data actually is.
My question is, how can we do this? This would be easy in Python, for example, but C++ doesn't like how mystruct
is "cross initialized", i.e. can have different types depending on which branch of the switch statement is used. Templating was an idea that we had wrestled with, but it's difficult because the names of the struct are different - e.g. struct intStruct
, struct floatStruct
. This is important because the size of these structs depends on the data type. Any ideas?