C++ Polymorphic Shared Pointer Container

Problem
We want to create a polymorphic container with pointers to different objects with the same base class. Furthermore, we want to have
 * 1) include
 * 2) include
 * 3) include

using namespace std;

struct A           { virtual int value { return 2; } }; struct B : public A { virtual int value { return 7; } };

class Index_manager { public: void add( int id, shared_ptr< A > a ) { index_map[ id ] = a; } shared_ptr< A > find( int id )       { return index_map[ id ]; } private: map< int, shared_ptr< A > > index_map; };

class Block_manager { public: void add( int id, shared_ptr< B > value ) { collection.push_back( value );

// use dynamic_pointer_cast to cast shared_ptr< B > // to shared_ptr< A > shared_ptr< A > a_ptr = dynamic_pointer_cast< A >( value ); index_manager.add( id, a_ptr ); }   shared_ptr< B > find( int id ) { shared_ptr< A > a_ptr = index_manager.find( id ); shared_ptr< B > value = dynamic_pointer_cast< B >( a_ptr ); return value; } private: list< shared_ptr< B > > collection; Index_manager index_manager; };

int main { Block_manager bm; bm.add( 0, shared_ptr< B >( new B ) ); shared_ptr< B > b = bm.find(0);

if( b.get == 0 ) return 0; // if b is empty, return 0 else              return b->value; } // returns 7