I’m currently trying to figure out good cases for multiple inheritance. Currently I’m writing a Mesh class
(OpenGL project). This Mesh class
has many useful methods and instance variables for other derived classes such as InstancedMesh class
and InstancedArrayMesh class
. So I think it’s a good idea to have both InstancedMesh class
and InstancedArrayMesh class
inherit from Mesh class
. But my issue occurs because both InstancedMesh class
and InstancedArrayMesh class
also share a Draw method
which is the exact same between them but differs from the Mesh class's Draw method
and thus it overrides that method. Should I have a fourth class called InstancedMeshCommon class
which holds the Draw method
for the two derived classes? If this was the case would I have to have the Draw method
overridden and it call the InstancedMeshCommon class's Draw method
?
Here’s some code below to better explain the architecture:
class Mesh { public: Mesh(); virtual void Draw(); private: std::vector<Vertex> vertices; void setup(); }; class InstancedMesh : private Mesh { public: InstancedMesh (); virtual void Draw() override; // Same code as InstancedArrayMesh // How should above method be shared; }; class InstancedArrayMesh : private Mesh { public: InstancedArrayMesh (); virtual void Draw() override; // Same code as InstancedMesh // How should above method be shared; };
EDIT
I’ve now realized that the InstancedMeshCommons
class should probably be a separate class of which there is an instanced in both InstancedMesh class
and InstancedArrayMesh class
. Does anyone agree or disagree?