I need to respect the LSP while supporting multiple data types (templates can't be used) and I have a dilemma between inheritance and composition. If I use inheritance, the base class cannot be used by itself, it must be casted everytime. Does that violate the LSP?
Inheritance would be:
enum eDataType{
FLOAT,
INT,
}
class IObject{
eDataType dataType;
...
};
class IntObject : IObject{
int paramA;
int paramB;
...
};
class FloatObject : IObject{
float paramA;
float paramB;
...
};
While using composition it would look like:
struct IntObject{
int A;
int B;
}
struct FloatObject{
float A;
float B;
};
class Object{
eDataType dataType;
IntObject* intObject;
FloatObject* floatObject;
Object(eDataType dataType){
if(dataType == FLOAT){ floatObject = new FloatObject();}
...
}
void updateData(Message* incomingMessage){
if(dataType == FLOAT)
...
};