|
Boost Users : |
Subject: Re: [Boost-users] Returning a ptr_vector of an abstract data type
From: Bill Buklis (boostusr_at_[hidden])
Date: 2011-04-08 13:35:40
On 4/8/2011 8:43 AM, Jim Hodapp wrote:
> I am using a ptr_vector<MyAbstractType> in a class called Container to
> store pointers to derived types inherited from MyAbstractType. In my
> Container class, I want to have a member function which returns the
> ptr_vector<MyAbstractType>. First, am I better off modelling this
> "getter" as an iterator over this ptr_vector or is it proper to return
> the ptr_vector itself? Right now, when trying to return the ptr_vector
> itself (a copy of it) I am getting an error:
> "/usr/include/boost/ptr_container/clone_allocator.hpp:34: error:
> cannot allocate an object of abstract type 'MyAbstractType'
>
> The function prototype for the getter is: ptr_vector<MyAbstractType>
> GetDestinations() const
>
> Is ptr_vector trying to make a copy of each MyAbstractType contained
> instead of copying the pointer value itself? This would indicate that
> I need a get() type method for ptr_vector like what a scoped_ptr has.
> Any recommendations on how to design this situation?
>
> Thanks,
>
> Jim
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
Yes, returning a copy of ptr_vector makes a copy of all of its objects
for the same reason that std::vector<object> does. It "owns" all of the
objects. It is not the same as std::vector<object*>. You could return a
reference instead to the ptr_vector if you truly don't need a copy.
Using iterators, though, would potentially make it more generic and then
easier to pass into other standard algorithms.
-- Bill
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net