By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,438 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

vector with deep copy

P: n/a
Is there anything like vector in STL, that performes deep copy of the
elements it contains?

I hope this will appear in future releases of STL :)
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Gunnar G wrote:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?

I hope this will appear in future releases of STL :)


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? Can you guarantee that none of the
original pointers was null, and that all of them pointed to valid
objects? 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?
Jul 22 '05 #2

P: n/a
> 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?
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.
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?

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

Jul 22 '05 #3

P: n/a
On Mon, 10 May 2004 11:05:29 GMT, Gunnar G <de****@comhem.se> wrote:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?


std::vector does perform deep copies - it copies contained elements
using their copy constructors.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4

P: n/a
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?
Jul 22 '05 #5

P: n/a
> std::vector does perform deep copies - it copies contained elements
using their copy constructors.

and if I copy a

vector<Foo*> x ?

Will it not only copy the addresses of the Foo elements? That is AFAIK a
shallow copy.
Jul 22 '05 #6

P: n/a
In article <Zl********************@newsb.telia.net>,
Gunnar G <de****@comhem.se> wrote:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?

I hope this will appear in future releases of STL :)


vector already does this. Simply hold a vector of objects that perform
deep copy when copied and vector will perform a deep copy of each object.
Jul 22 '05 #7

P: n/a

Uzytkownik "Gunnar G" <de****@comhem.se> napisal w wiadomosci
news:oX********************@newsc.telia.net...
std::vector does perform deep copies - it copies contained elements
using their copy constructors.

and if I copy a

vector<Foo*> x ?

Will it not only copy the addresses of the Foo elements? That is AFAIK a
shallow copy.


Yes it will copy only addresses. But that is what it contains - so this is
actually a 'deep copy'. std::vector does not dereference the stored pointers
in any way and treats them exactly as if they were, say, ints.

If you want std::vector to copy objects, create a vector with objects, not
with pointers to them:

std::vector<Foo> x;

regards,
Marcin
Jul 22 '05 #8

P: n/a
> 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?


I guess we have been talking about different things, Yes I'm gonna write my
own container, I just asked if someone had done this before me so I didn't
have to reinvent the wheel :-)

Anyway, std::vector does not make a deep copy (as I define deep copy), at
least not with gcc-3.0. If it did a deep copy, it would give different
adresses below.

#include <iostream>
#include <vector>

using namespace std;

class Foo{
public:
int x;
Foo(){x=1;}
Foo(int q):x(q){}
};

int main(){
vector<Foo*> a,b;
Foo* t=new Foo(3);
a.push_back(t);
cout<<"Adress "<<a[0]<<endl;
b=a;
cout<<"Adress "<<b[0]<<endl;
}
Jul 22 '05 #9

P: n/a
In article <Ds********************@newsc.telia.net>,
Gunnar G <de****@comhem.se> wrote:
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?


I guess we have been talking about different things, Yes I'm gonna write my
own container, I just asked if someone had done this before me so I didn't
have to reinvent the wheel :-)

Anyway, std::vector does not make a deep copy (as I define deep copy), at
least not with gcc-3.0. If it did a deep copy, it would give different
adresses below.

#include <iostream>
#include <vector>

using namespace std;

class Foo{
public:
int x;
Foo(){x=1;}
Foo(int q):x(q){}
};

int main(){
vector<Foo*> a,b;
Foo* t=new Foo(3);
a.push_back(t);
cout<<"Adress "<<a[0]<<endl;
b=a;
cout<<"Adress "<<b[0]<<endl;
}


Another approach you might consider besides implementing a container is
implementing a smart pointer: copy_ptr<T>:

copy_ptr<Foo> a(new Foo(3));
copy_ptr<Foo> b(a); // makes copy with new Foo(const Foo&)
assert(a != b);

Then you can just:

vector<copy_ptr<Foo> > va;
va.push_back(copy_ptr<Foo>(new Foo));

An advantage to this approach is that if you decide you need different
semantics on copy, it is easier to change to a new smart pointer rather
than rewrite your container. For example if Foo is a base class with a
virtual clone function (typical virtual copy ctor) you could create a
clone_ptr:

clone_ptr<Foo> a(new DerivedFromFoo(3));
clone_ptr<Foo> b(a); // makes copy with a->clone()
assert(a != b);
....
vector<clone_ptr<Foo> > vec_of_base_pointers;

A high quality refcounted pointer is available at boost (www.boost.org).
The same design is also in the first library technical report and thus
may be available with your C++ compiler under namespace std::tr1 (e.g.
Metrowerks Pro 9). So you could:

vector<std::tr1::shared_ptr<Foo> > shared_vec;

-Howard
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.