I saw some other questions on this topic, but still have not found an answer - in my opinion, I missed something:
I defined two simple test classes:
class TestBase { public: TestBase ( ) { }; ~ TestBase ( ) { }; protected: inline virtual int getInt ( ) { return 0; } }; class TestDerived : public TestBase { protected: inline int getInt ( ) override { return 1; } };
I declared typedefs to simplify their use with std::shared_ptr:
typedef std::shared_ptr<TestBase> spBase; typedef std::shared_ptr<TestDerived> spDerived;
Problem: I cannot compile code to use these shared_ptr declarations polymorphically, although base in all of these cases is actually an instance of spDerived :
spBase base; spDerived derived = static_cast < spDerived > ( base );
error: there is no corresponding function to call in 'std :: shared_ptr :: shared_ptr (spBase &)
spDerived derived = dynamic_cast < spDerived > ( base );
error: cannot dynamic_cast base '(of type' spBase {aka class std :: shared_ptr}) for input 'spDerived {aka class std :: shared_ptr} (target is not a pointer or link)
spDerived derived = static_pointer_cast < spDerived > ( base );
error: conversion from 'std :: shared_ptr> to non-scalar type' SpDerived {aka std :: shared_ptr} requested
spDerived derived = dynamic_pointer_cast < spDerived > ( base );
error: conversion from 'std :: shared_ptr> to non-scalar type' SpDerived {aka std :: shared_ptr} requested
I use C ++ 11 in an Ubuntu 14.04 box with the standard GCC toolchain. The compiler is gcc-4.9. What am I doing wrong? Is it possible to use the shared_pointer parameter polymorphically?
gcc polymorphism casting c ++ 11 shared-ptr
Vector
source share