|
Boost Users : |
Subject: Re: [Boost-users] Returning a ptr_vector of an abstract data type
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-04-08 12:28:49
2011/4/8 Jim Hodapp <james.hodapp_at_[hidden]>
> 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<MyAbstractType,MyCloneAllocator>.
Regards,
Kris
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