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.
class MyObject : virtual public NewAbstract, virtual public OldAbstract { ... }
class MyObject : public OldAbstract { ... }
class NormalObject : public ISerializable
class NewAbstract : public ISerializable { ... }
class OldAbstract : public ISerializable { ... }
template<class T> class Factory
{
public:
...
virtual ISerializable* createObject() const
{
return static_cast<ISerializable*>(new T());
}
...
}
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.
? , .