Is conversion from Base* to Derived* always allowed ? Is conversion from const Base* to const Derived* always allowed ?
I am using Qt QSharedPointers. I have a QSharedPointer to Base object, that I want to convert to a QSharedPointer to a Derived object. I check beforehand that pointee is of type Derived.
Is it possible with 100% chance of success with
static_cast<QSP<Derived>>dynamic_cast<QSP<Derived>>- a direct cast
QSP<Derived>(my_base_pointer)
?
For example, for direct conversion:
QSP<Derived> inpf = QSP<Derived>(my_base_pointer) ;
i have error
Error 1 error C2440: 'initializing' : cannot convert from 'Base *' to 'Derived *'
with my_base_pointer of type const QSP<Base>.
Is it because of constness ? Should I const_cast on top of that ? I guess constness is not root cause, since if i
const QSP<Derived> inpf = static_cast<const QSP<Derived>>(my_base_pointer);
same error occurs.