Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

templates, shared_ptr.. got casting problems

Started by murderv Jun 9, 2010 at 2:50 AM 1 replies 1.6k views
Original Post
murderv
murderv
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

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' 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
Zipster
Zipster
Quote:
Original post by murderv
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.

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 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.
murderv
murderv
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.