You cannot use a class with multiple inheritance

I am trying to reorganize some code while still leaving valid functionality. I am having trouble casting a pointer to an object in the base interface, and then get the derived class later. The program uses the factory object to create instances of these objects in certain cases.

The following are examples of the classes I'm working with.

// This is the one I'm working with now that is causing all the trouble.
// Some, but not all methods in NewAbstract and OldAbstract overlap, so I
// used virtual inheritance.
class MyObject : virtual public NewAbstract, virtual public OldAbstract { ... }

// This is what it looked like before
class MyObject : public OldAbstract { ... }

// This is an example of most other classes that use the base interface
class NormalObject : public ISerializable

// The two abstract classes. They inherit from the same object.
class NewAbstract : public ISerializable { ... }
class OldAbstract : public ISerializable { ... }

// A factory object used to create instances of ISerializable objects.
template<class T> class Factory
{
public:
    ...
    virtual ISerializable* createObject() const
    {
        return static_cast<ISerializable*>(new T()); // current factory code
    }
    ...
}

This question contains good information about what different types of castings do, but it does not help me figure this out. Using static_cast and regular casting, give me error C2594: 'static_cast': ambiguous conversions from 'MyObject *' to 'ISerializable *'. Using dynamic_cast calls createObject () to return NULL. The NormalObject style classes and the old version of MyObject work with the existing static_cast in the factory.

? , .

+5
4

ISerializable ( VS2010). , Diamond Problem, , .

EDIT:

:

class NewAbstract : public virtual ISerializable { ... } 
class OldAbstract : public virtual ISerializable { ... } 
+10

, , .

virtual ISerializable* createObject() const
{
    NewAbstract*const na = dynamic_cast< NewAbstract* >( new T() );
    return dynamic_cast< ISerializable* >( na );
}
+1

NewAbstract, OldAbstract. , . , .

0

" " . .

-2
source

All Articles