Re: vector with deep copy
From: Jeff Schwab (jeffplus_at_comcast.net)
Date: 05/10/04
- Next message: Gunnar G: "Re: vector with deep copy"
- Previous message: heinquoi: "Re: using of "static const" in class with no imediately init"
- In reply to: Gunnar G: "Re: vector with deep copy"
- Next in thread: Gunnar G: "Re: vector with deep copy"
- Reply: Gunnar G: "Re: vector with deep copy"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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?
- Next message: Gunnar G: "Re: vector with deep copy"
- Previous message: heinquoi: "Re: using of "static const" in class with no imediately init"
- In reply to: Gunnar G: "Re: vector with deep copy"
- Next in thread: Gunnar G: "Re: vector with deep copy"
- Reply: Gunnar G: "Re: vector with deep copy"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|