   templates, shared_ptr.. got casting problems
  Hi there,Im having problems understanding how to handle templates and type casting so i can save an array of create objects with different types, but all have same base class.Im trying to create a few classes that will wrap different object types which have different initialization parameters, so i thought i would use a template for that. the different objects come from Bullet and are plane, sphere, cylinder, etc.So i wrote a template class from which others will derive from with different types:Later on i would like to save this list of objects in a vector or array within a manager (using shared_ptr). My problem is handling the casting to save all objects within an vector of shared_ptr. II still haven't got that figured out, and i would like your help on that. If you find this method wrong, i'd like to hear about others tooSo here goes:The template class, from where every other object derive fromtemplate<typename T>class Object{  // shared members  T *shape;  // methods  Object( string name );  void process(); // shared methods  void update( time );}The derived object classes/// PLANEclass Plane : public Object<Physics::Plane>{  Plane( string name ) {}  Create( vector3 normal, vector3 point );  {shape = new Physics::Plane( normal, point );// code here// and...process();  }}/// SPHEREclass Sphere: public Object<Physics::Sphere>{  Sphere( string name )  {}  Create( vector3 center, float radius )  {shape = new Physics::Sphere( center, rad );// code here// and...process();  }}// And now the Manager classtypedef vector< shared_ptr< Object<class T> > >  ObjectListPtr;class ObjectManager{  // members  ObjectListPtr _ObjectList;  // methods  Sphere* CreateSphere( vector3 center, float rad )  {return CreateSphere( center, rad ).get();  }  shared_ptr<Sphere> CreateSphere( string name, vector3 center, float rad )  {    shared_ptr<Sphere> obj = shared_ptr<Sphere>( new Sphere(name) );    obj->Create( center, rad );    // **ERROR**// THIS IS IT!! How to handle the casting and save all different objects to a common vector list ?    _ObjectList.add( obj );    return obj;  }}And this is the kind of errors i get:error C2440: 'initializing' : cannot convert from 'boost::shared_ptr<T>' to 'boost::shared_ptr<T>'2>with2>[2>    T=Plane2>]2>and2>[2>    T=Object<T>2>]2>No constructor could take the source type, or constructor overload resolution was ambiguous   
Quote:Original post by murdervIm having problems understanding how to handle templates and type casting so i can save an array of create objects with different types, but all have same base class.The issue is that they don't all have the same base class.  What you have here is known as the CRTP idiom.  Simple put, every derived class you have creates its own special base class, however none of the base classes are related (even though they share template code).  So as things are now, there is no way to cast between them because each shape exists in its own isolated mini-hierarchy.The solution is to either remove the templates from Object, or create a base class above Object<T> so that all these types belong to the same hierarchy.  From what I can see you don't really need to use CRTP, but that's only based on what I can see.   ;  Hi Zipster,Thanks for your reply. I did not know about CRTP.Well the idea to use template was only to not have a define type of "shape" as it can be several things as plane, sphere, cylinder, cone, elliptical, etc etc.That was the only reason i went on using template.On the physics side, each object have a base class too called Shape.Maybe i can remove this template code and just do, Shape *shape; in the Object class and for the derived ones downcast their types ???What other elegant solution would you present to me ?thanks again.   