vector with deep copy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gunnar G

    vector with deep copy

    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 :)
  • Jeff Schwab

    #2
    Re: vector with deep copy

    Gunnar G wrote:[color=blue]
    > 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 :)[/color]

    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?

    Comment

    • Gunnar G

      #3
      Re: vector with deep copy

      > That depends how deep you want the copy to be... If a vector contains[color=blue]
      > pointers, do you want the new copy to contain new pointers to copies of
      > the originally referenced elements?[/color]
      That's whats meant by deep copy, isn't it?
      Anyway, that is what I want.
      [color=blue]
      > Can you guarantee that none of the original pointers was null,
      > and that all of them pointed to valid objects?[/color]
      Null pointers should never be stored in the container, so yes, that is
      guaranteed.
      [color=blue]
      > 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?[/color]
      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.

      Comment

      • tom_usenet

        #4
        Re: vector with deep copy

        On Mon, 10 May 2004 11:05:29 GMT, Gunnar G <debian@comhem. se> wrote:
        [color=blue]
        >Is there anything like vector in STL, that performes deep copy of the
        >elements it contains?[/color]

        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

        Comment

        • Jeff Schwab

          #5
          Re: vector with deep copy

          Gunnar G wrote:[color=blue][color=green]
          >>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?[/color]
          >
          > That's whats meant by deep copy, isn't it?[/color]

          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.
          [color=blue]
          > Anyway, that is what I want.
          >
          >[color=green]
          >>Can you guarantee that none of the original pointers was null,
          >>and that all of them pointed to valid objects?[/color]
          >
          > Null pointers should never be stored in the container, so yes, that is
          > guaranteed.[/color]

          You mean under your particular circumstances, or are you asserting that
          null pointers should never be stored in std::vector?
          [color=blue][color=green]
          >>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?[/color]
          >
          > 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?[/color]

          No, everything is not then perfectly happy.
          [color=blue]
          > I've read something about a virtual constructors in Bjarne's book, guess
          > I'll have to read more.[/color]

          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?

          Comment

          • Gunnar G

            #6
            Re: vector with deep copy

            > std::vector does perform deep copies - it copies contained elements[color=blue]
            > using their copy constructors.[/color]
            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.

            Comment

            • Daniel T.

              #7
              Re: vector with deep copy

              In article <ZlJnc.58692$mU 6.237676@newsb. telia.net>,
              Gunnar G <debian@comhem. se> wrote:
              [color=blue]
              >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 :)[/color]

              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.

              Comment

              • Marcin Kalicinski

                #8
                Re: vector with deep copy


                Uzytkownik "Gunnar G" <debian@comhem. se> napisal w wiadomosci
                news:oXJnc.9213 9$dP1.288032@ne wsc.telia.net.. .[color=blue][color=green]
                > > std::vector does perform deep copies - it copies contained elements
                > > using their copy constructors.[/color]
                > 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.[/color]

                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


                Comment

                • Gunnar G

                  #9
                  Re: vector with deep copy

                  > At this point, you're already imposing some pretty tough rules on the[color=blue]
                  > 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?[/color]

                  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;
                  }

                  Comment

                  • Howard Hinnant

                    #10
                    Re: vector with deep copy

                    In article <DsKnc.92143$dP 1.287973@newsc. telia.net>,
                    Gunnar G <debian@comhem. se> wrote:
                    [color=blue][color=green]
                    > > 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?[/color]
                    >
                    > 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;
                    > }[/color]

                    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(co py_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_pt r<Foo> > vec_of_base_poi nters;

                    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<Fo o> > shared_vec;

                    -Howard

                    Comment

                    Working...