Let's say I have this Haskell code:
data RigidBody = RigidBody Vector3 Vector3 Float Shape -- position, velocity, mass and shape
data Shape = Ball Float -- radius
| ConvexPolygon [Triangle]
What would be the best way to express this in C++?
struct Rigid_body {
glm::vec3 position;
glm::vec3 velocity;
float mass;
*???* shape;
};
The thing I'm asking is how to represent shape inside the struct when it can be one of two types.
There are different approaches that can be used to solve that problem in C++.
The pure-OO approach you would define an interface
Shapeand have the two different options as derived types implementing that interface. Then theRigidBodywould contain a pointer to aShapethat would be set to refer to either aBallor aConvexPolygon. Pro: people love OO (not sure this is a pro really :)), it is easily extensible (you can add more shapes later on without changing the type). Con: You should define a proper interface forShape, it requires dynamic allocation of memory.Putting OO aside, you can use a
boost::variantor similar type, that is basically a tagged union that will hold one of the types. Pro: no dynamic allocations, shape is local to the object. Con: not pure-OO (people love OO, you remember right?), not so easy to extend, cannot use the shape generically