in below scenario, I need to invoke child class B function (fun1) from Base class A shared pointer returned by setup function and for the same have used dynamic_cast_pointer so that derived class shared_ptr object can be assigned to Base class shared_ptr but during compilation I am not allowed to do so. Can anybody suggest how to achieve this behaviour
#include <iostream>
#include <memory>
using namespace std;
class A
{
public:
void fun() {cout<<"A"<<endl;}
};
class B : public A
{
public:
void fun1() {cout<<"B" <<endl;}
};
shared_ptr<A> setup(shared_ptr<A> ptr = nullptr )
{
if(!ptr)
{
ptr = make_shared<A> ();
}
ptr.get()->fun();
return ptr;
}
int main() {
auto ptr = std::dynamic_pointer_cast <B> (setup());
ptr->fun1(); // Doesn't compile
}