Returning a ptr_vector of an abstract data type
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
2011/4/8 Jim Hodapp
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
I think you should read about the cloneable concept in ptr_container docs,
which kicks in when you make a copy of a pointer container.
Typically (if I remember correctly) you need to overload make_clone, and
delete_clone for your MyAbstractType. Or you may write a clone allocator,
and use ptr_vector
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@lists.boost.org 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
participants (3)
-
Bill Buklis
-
Jim Hodapp
-
Krzysztof Czainski