how model class general , specific attributes based on type. example have primitive class. primitive class has following general members : primitivetype, translation, rotation, plus additional fields based on primitive type.
enum primitivetype {    cylynder,    cube,    cone } class primitive {    string name;    primitivetype type;    double positionx,positiony,positionz;    double rotationx,rotationy,rotationz;    // following members if primitivetype cylynder    double height;    double radius;    //following members if primitive cube   double height;   double width;   double length;  };   i can of course inheritance , make primitive , cylynder , cube classes inherit primitive. classes have not polymorphic relationships between themselves, not use inheritance. need them plain structures hold attributes.
i can make composition , make cylynder , cube classes have primitive member. need store objects of cylynder,cube , cone in 1 vector. if composition how store them in 1 std::vector .
i need model structures in such way need met following requirements :
1) store objects of different types of components in 1 std::vector
2) store object of different types in easy readable , editable config file. in config file want save specialized attributes relevant specific type of primitive, , not specialized attributes primitive types. want in config file :
<primitive>  <name>primitive1</name>  <type>cylynder</type>  <positionx>0</positionx>  <!-- other common attributes here, omitted 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 save space -->    <!-- specific primitive type attributes -->   <height>10</height>   <width>10</width>   <length>10</length> </primitive>      
you have many choices, e.g.:
create 3 distinct classes , store
boost::variant<cylinder, cube, cone>s invector(conceptually that's discriminated union,boost::variantcleans , handles ugly edge cases alignment)- you can still use composition shared members/functionality if desired
 
create 1 class enum , "fat" interface , fields (as you've started above, removing duplicate height "// many primitives" group)
- hackish , entangled, if don't have many primitivetypes deal with, , they're not complex, may manageable , practical
 
use polymorphism - seems there'll common subset of functionality can exposed, , while it's ugly
dynamic_cast<>supports runtime-type-specific switching- your "have not polymorphic relationships between themselves, not use inheritance" isn't compelling justification avoiding it, given claimed need store types in single 
vector 
- your "have not polymorphic relationships between themselves, not use inheritance" isn't compelling justification avoiding it, given claimed need store types in single 
 
as creating objects file content... need called "factory" - can read primitivetype config file call type-specific field-parsing , construction code.
Comments
Post a Comment