Having the following structures:
class XY
{
private:
int test;
};
class XYZ
{
private:
short extra;
int test;
};
I want to use XY::test and XYZ::test depending on a runtime variable. Like:
if (x == 1)
{
reinterpret_cast<XY*>(object)->test = 1;
}
else if (x == 2)
{
reinterpret_cast<XYZ*>(object)->test = 1;
}
Is it possible to make this into a nice one-liner? (using templates, macros, etc...?)
What should I do about &object->test depending on a different type of object? What if the object is passed into a function like the following?
void Function(void* object)
{
if (x == 1)
{
reinterpret_cast<XY*>(object)->test = 1;
}
else if (x == 2)
{
reinterpret_cast<XYZ*>(object)->test = 1;
}
}
How can I properly deal with this? I have so much code like this, so I can't write an if condition every time. Also, I am unable to change the structures' layout in any way, shape, or form.