I'm working on a custom engine where I have 2 different transform classes, one for 2D and one for 3D. I'm using a #define to choose which transform class to use and using that definition instead of the classname in places where the logic should be the same. I am at a part now where I want them to have different logic and wanted to do a comparison to branch off for that. What do I need to do to get this to work?
class Transform2D;
class Transform3D;
#define TransformClass Transform2D
if(TransformClass == Transform2D)
{
//like this
}
else
{
//like that
}
Type id worked for that. How do you handle?
if ( typeid(TransformClass) == typeid(Transform2D) )
{
ittransform->SetRotation(0);
ittransform->SetScale(Vector2D(defaultScale, defaultScale));
}
else
{
ittransform->SetRotation(Vector3f());
ittransform->SetScale(Vector3f(defaultScale, defaultScale, defaultScale));
}
Use function decomposition (break your logic into sub-functions) and then make use of overloading.
Also, use a typedef or type alias instead of a define, e.g.
You can also define the alias via std::conditional and be even more functional-y and C++-y.