开发者

Doubts using an object pool

开发者 https://www.devze.com 2023-03-17 17:59 出处:网络
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 desir

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 a开发者_开发技巧ncestor 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消