Re: vector with deep copy

From: Jeff Schwab (jeffplus_at_comcast.net)
Date: 05/10/04


Date: Mon, 10 May 2004 07:46:57 -0400

Gunnar G wrote:
>>That depends how deep you want the copy to be... If a vector contains
>>pointers, do you want the new copy to contain new pointers to copies of
>>the originally referenced elements?
>
> That's whats meant by deep copy, isn't it?

Not necessarily. The elements "contained" by a std::vector aren't
actually members of the container class, they're held in memory pointed
to by a member variable. Since the referenced memory is copied, a
vector already is doing a deep copy.

> Anyway, that is what I want.
>
>
>>Can you guarantee that none of the original pointers was null,
>>and that all of them pointed to valid objects?
>
> Null pointers should never be stored in the container, so yes, that is
> guaranteed.

You mean under your particular circumstances, or are you asserting that
null pointers should never be stored in std::vector?

>>If the original pointers were of type pointer-to-A, but some
>>of the objects actually were of some subclass of A, how do you avoid
>>slicing?
>
> Well, I have to read more about slicing, but I would certainly want to have
> an object of the subclass in the copy if that is what was in the original.
> Can't I assume that the user is using virtual functions in their classes,
> and then everything is perfectly happy?

No, everything is not then perfectly happy.

> I've read something about a virtual constructors in Bjarne's book, guess
> I'll have to read more.

At this point, you're already imposing some pretty tough rules on the
types of element that can be stored in the vector. It sounds like you
need an "intrusive" container, very different from std::vector. Why
don't you implement your own, possibly using std::vector as part of the
implementation, and post your code here for feedback?



Relevant Pages

  • Re: Idioms for iterators in C
    ... I think returning a deep copy of the contained object is overkill. ... container is purpose-built for the data type, ... pointers around because he thinks they threaten security;-) ... should try not to invalidate any existing iterator. ...
    (comp.lang.c)
  • Re: Idioms for iterators in C
    ... I think returning a deep copy of the contained object is overkill. ... people tend to overestimate the cost of deep copies. ... container is purpose-built for the data type, ... pointers around because he thinks they threaten security;-) ...
    (comp.lang.c)
  • Re: Idioms for iterators in C
    ... I think returning a deep copy of the contained object is overkill. ... container is purpose-built for the data type, ... pointers around because he thinks they threaten security;-) ... There are two types of containers: value containers and reference ...
    (comp.lang.c)
  • Re: Idioms for iterators in C
    ... I think returning a deep copy of the contained object is overkill. ... container is purpose-built for the data type, ... pointers around because he thinks they threaten security;-) ... should try not to invalidate any existing iterator. ...
    (comp.lang.c)
  • Re: a question of style
    ... What do you mean with "a shallow copy"? ... those pointers; ... a deep copy creates a new copy of any pointed-to data ... A deep copy requires knowing the contents of the structure. ...
    (comp.lang.c)