Re: Shallow\Deep copy
On Oct 7, 6:14 am, ManicQin <manic...@gmail.com> wrote:
I tried to think of a way to deep copy the vector into another ->without<-
adding Clone function and such to the objects and I couldnt
think of one, I would be happy if anyone knows of a way...
What's not great with your idea is that you get deep or shallow copy
depending on whether Interface "implements" ICloneable or not, which
might end up in a surprise for the user. But hey, it's simple and will
work with that small constraint!
You could also add "cloning traits" template parameter to your
container. Duty of that thing would be to create a clone of your
items ;-). As default, you could use the simplest possible: copy-
construction (but, that doesn't work if Interface is abstract). You
can also offer ICloneable-using version. E.g.
template<class Interface> struct DefaultCloningTrait
{ // Default: needs "copy-constructible"
Interface* Clone(const Interface& Item) const { return new Interface
(Item); }
};
template<class Interface, class
CloningTrait=DefaultCloningTrait<Interface> >
class CompositorBase
{
CloningTrait _CloningTrait;
public:
CompositorBase(CloningTrait ct=CloningTrait()) : _CloningTrait(ct)
{};
Interface* CloneItem(const Interface& Item) { return
_CloningTrait.Clone(Item); }
};
template<class Interface> struct CloneableCloningTrait
{ // Using ICloneable interface
Interface* Clone(const Interface& Item) const { return
dynamic_cast<const ICloneable&>(Item).Clone(); }
};
Goran.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]