0

I use the virtual inheritance.

I basically have the "tree" of inheritance for some abstract module:

class Positionable{
    public:
        virtual std::string getName() = 0;
};

class Positionable3D : virtual public Positionable{
    public:
        virtual void setCenter(Point3D &center) = 0;
};

class Renderable3D : virtual public Positionable3D{
    public:
        virtual void render() = 0;
};

I write the implementation of it, using my own structures that inherit from them:

class FeyPositionable : virtual public Positionable{
    public:
        virtual std::string getAdditionalInfo() = 0;
};

class FeyPositionable3D 
  : virtual public Positionable3D, virtual public FeyPositionable{

    public:
        virtual void someFun() = 0;
};

class FeyRenderable3D 
  : virtual public Renderable3D, virtual public FeyPositionable3D{
    protected:
        GeometryStructure * structure;

    public:
        virtual void render() = 0;
};

But when I compile it and run in Visual C++ 2012, in debug window I see many duplicates of functions and members, for example:

enter image description here

Is it only the debugger window "problem", or I have multiple copies of methods/members? If so, can they have different values (e.g. copies of structure)?

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • 1
    You're deriving from the base classes multiple times through multiple paths, so they're appearing multiple times. – Mark Ransom Mar 05 '14 at 17:31
  • But does it causes to duplicate some methods or members in program? I mean e.g. it uses more amount of memory (for that duplicates)? Or it just cause debugger to looks so unreadable? Should I rebuild it somehow or it's ok? – PolGraphic Mar 05 '14 at 17:34
  • 1
    Can you show declaration of the class that is shown in debug window? I mean, the declaration of the type of `this`. It looks like it inherits from some `FeyModel` and bunch of other classes. – lapk Mar 05 '14 at 18:04

1 Answers1

0

Okay, so by using virtual inheritance, you have only one instance of Positionable created for anything that is derived from either Positionable3D or FeyPositionable.

However, you also virtually inherit from Positionable3D with Renderable3D, and you virtually inherit from Positionable3D and FeyPositionable for the class FeyPositionable3D.

This means that when you create an instance of FeyRenderable3D, based on this reference, you are creating a storable instance of FeyRenderable3D, an instance of Renderable3D and FeyPositionable3D, and an instance of all of their parents. Now, each time you virtually inherit, you are ensuring that no matter who inherits from who, only one instance of each class in your hierarchy is created. So, in conclusion, there should be no issue with your implementation, but you are creating a pretty wild set of vtables with virtual pointers criss-crossing all over the place.

thealmightygrant
  • 701
  • 1
  • 5
  • 11
  • Corrected my post. FeyPositionable3D inherits from Positionable3D (from my "abstract" tree) and FeyPositionable (not Positionable). I have to have access to FeyPositionable::getAdditionalInfo() and Positionable::getName() as well. – PolGraphic Mar 05 '14 at 18:36
  • Also, you need to make sure that some class isn't virtual so that an instance of it can actually be created. The only non-virtual class in your hierarchy is `FeyRenderable3D` and this is an abstract class because of the function `render()=0`. So, this class cannot be instantiated either. – thealmightygrant Mar 05 '14 at 22:04