How to model a class with general and specific attributes based on type. For example I have a Primitive class. The Primitive class has the following general members : PrimitiveType, translation, rotation, plus additional fields based on the primitive type.
enum PrimitiveType
{
CYLYNDER,
CUBE,
CONE
}
class Primitive
{
string name;
PrimitiveType type;
double positionX,positionY,positionZ;
double rotationX,rotationY,rotationZ;
// following members are if primitivetype is CYLYNDER
double height;
double radius;
//following members are if primitive is CUBE
double height;
double width;
double length;
};
I can of course do inheritance and make Primitive and Cylynder and Cube classes that inherit from Primitive. But classes have not polymorphic relationships between themselves, so I do not to use inheritance. I need them as just plain structures that hold attributes.
I can make also composition and make Cylynder and Cube classes that have Primitive member. But I need to store objects of Cylynder,Cube and Cone in one vector. So if I do composition how I will store them in one std::vector .
I basically need to model the structures in a such way that I need to met the following requirements :
1) store objects of the different types of components in one std::vector
2) store object of different types in a easy readable and editable config file. In the config file I want to save only the specialized attributes that are relevant for the specific type of the primitive, and not the specialized attributes for all primitive types. So I want to get something like this in the config file :
<Primitive>
<name>Primitive1</name>
<type>CYLYNDER</type>
<positionx>0</positionx>
<!-- other common attributes here, omitted to save space -->
<!-- specific primitive type attributes -->
<height> 10 </height>
<redius>5</radius>
</Primitive>
<Primitive>
<name> Primitive2 </name>
<type> CUBE </type>
<positionx>0</positionx>
<!-- other common attributes here, omitted to save space -->
<!-- specific primitive type attributes -->
<height>10</height>
<width>10</width>
<length>10</length>
</Primitive>
Aucun commentaire:
Enregistrer un commentaire