Pergunta

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)?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top