0

I'm thinking in use a boost::object_pool, but the types of objects to store are all in the same hierarchy. My question is how do I need to store and use the pool to convert each object into the desired type.

My guest that store the ancestor as a type of the pool, then convert the returned object with a dinamyc cast to the proper type.

Is it an object pool the best alternative??

Need some orientation :) Thanks in advance

EDIT: All of you are right. I was thinking in the traditional casting newObj = (newType)oldObj. Sorry.

Killrazor
  • 6,856
  • 15
  • 53
  • 69
  • 1
    "But then, I'm generating a new object" What!? That's not the way dynamic cast works. – Yakov Galka Jul 07 '11 at 10:05
  • 1
    I don't think you are generating a new object with dynamic_cast. You're casting one pointer to an object into a pointer of another type of object. – Ozair Kafray Jul 07 '11 at 10:05
  • 1
    I do not fully understand your scenario but I can say one thing: using `dynamic_cast` does not generate new objects. – Peter G. Jul 07 '11 at 10:07
  • Edited. I was thinking in old style cast – Killrazor Jul 07 '11 at 10:16
  • The 'old style' cast to a direct class type will create a new object but will not work as intended in the typical scenarios I imagine. That would be reverse slicing (http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c). The old style cast to a pointer type will work most of the time just like a dynamic cast, creating no object either. – Peter G. Jul 07 '11 at 17:08

2 Answers2

2

To store polymorphic objects, the idiomatic solution is to store pointers of a base class in a value based container.

Your current solution of storing polymorphic objects by value does not work because it suffers from slicing. The boost object pool is no more than a fancy allocator and deallocator, it does not provide polymorphism.

Community
  • 1
  • 1
Peter G.
  • 14,786
  • 7
  • 57
  • 75
1

boost::object_pool is primarily for allocating lots of objects of the same type. You shouldn't use them for object hierarchies.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • And what would be a viable alternative? Thanks. It would be a good idea a factory and some object pools, each containing their our objects? – Killrazor Jul 07 '11 at 10:18
  • @Killrazor: It's hard to say what an alternative might be without knowing what your allocation pattern for these objects is. What were you doing with them that made you think an object pool was necessary to begin with? – Nicol Bolas Jul 07 '11 at 10:39
  • What I'm doing is to put and to remove different kind of sprites into a set of objects called "layer". It could be an animated sprite or a text sprite, or a particle effect, so I don't want to waste time allocating sprites into a manager and casting it to the correct type. My idea is to make a factory that returns an instance of object of the correct type from the correct pool, so having a set of pools behind the factory seems good, at least apparently :) – Killrazor Jul 07 '11 at 12:57