Original Post
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. I
I 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 too
So here goes:
The template class, from where every other object derive from
The derived object classes
// And now the Manager class
And this is the kind of errors i get:
error C2440: 'initializing' : cannot convert from 'boost::shared_ptr' to 'boost::shared_ptr'
2> with
2> [
2> T=Plane
2> ]
2> and
2> [
2> T=Object
2> ]
2> No constructor could take the source type, or constructor overload resolution was ambiguous
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. I
I 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 too
So here goes:
The template class, from where every other object derive from
template<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 class
typedef 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
2> with
2> [
2> T=Plane
2> ]
2> and
2> [
2> T=Object
2> ]
2> No constructor could take the source type, or constructor overload resolution was ambiguous