How can i cast to a derived class? The below approaches all give the following error:
Cannot convert from BaseType to DerivedType. No constructor could take
the source type, or constructor overload resolution was ambiguous.
BaseType m_baseType;
DerivedType m_derivedType = m_baseType; // gives same error
DerivedType m_derivedType = (DerivedType)m_baseType; // gives same error
DerivedType * m_derivedType = (DerivedType*) & m_baseType; // gives same error
Think like this:
Now looking back at your first statement:
You should very rarely ever need to use dynamic cast.
This is why we have virtual methods:
The only reason I can think of is if you stored your object in a base class container:
But if you need to cast particular objects back to Dogs then there is a fundamental problem in your design. You should be accessing properties via the virtual methods.