c++pointersboostptr-vector

Get a pointer instead of a reference from a boost::ptr_vector


I recently found the boost ptr_vector useful to manage my collection of heap-allocated objects. The pointer collection library is very nice, but unfortunately, I'm being held up by one thing.

Another part of my code needs to explicitly hold a pointer to one of my objects in the ptr_vector (for specific reasons it cannot be a reference). However, when you access an object in a ptr_vector, you get a reference, T& (even though you used ptr_vector.push_back(T *)

Is there anyway I can get a plain pointer out of a boost::ptr_vector?


Solution

  • Yes,

    boost::ptr_vector<int> v;
    v.push_back(new int());
    int* ptr = &v[0];