I am pulling my hair out trying to get smart_ptrs to work with STL.
I'm tempted to use:
1 2 3 4 5 6 7 8
class MyClass {
vector<OtherClass*> m_ptrs;
void addNewObj() { m_ptrs.push_back( new OtherClass() ); }
const OtherClass& getObj( size_t idx ) {
if ( idx<m_ptrs.size() )
return *(m_ptrs[ idx ]);
}
}
and just "do the right things" in the destructor (loop and delete), and block-out the copy-constructor and assignment operator by making them private...
Is there a way to make m_ptrs work with smart_ptrs or is it just not worth the trouble?!?
I really have no intention to "share" my_ptrs with the client - just allowing const access is sufficient...
If I use a plain new to allocate, boost::ptr_vector seems like the best bet.
But in this special case, I am allocating a lot of elements, so I need to use boost::object_pool.
If boost::ptr_vector also owns its items (ie deletes them on destruct), I probably have to use one or the other (either ptr_vector or object_pool, but not both).
I only once tried to use part of the pool library for other things but ran into some weird problems probably related to the fact that I was using an older compiler. So anyway, I'm not really familiar with object pools. Why do you need to use them? (What are object pools buying you? Is it to avoid the expensive memory allocation because you are frequently newing/deleting objects?)