How to overload operator= to have both deep copy and shallow copy semantics

Discussion in 'C++' started by bluekite2000@gmail.com, Jun 24, 2005.

  1. Guest

    In other words

    Array<int,1> A(5), B(10);
    A = B(Range(0,4)); // Statement 1
    Array<int,1> C = B(Range(0,4)); // Statement 2

    I d like
    Statement 1 results in a portion of B's data being copied into A
    and
    After Statement 2 is executed, the array C is a reference (or alias) to
    B's data.
    Regards,
     
    , Jun 24, 2005
    #1
    1. Advertising

  2. Re: How to overload operator= to have both deep copy and shallowcopy semantics

    wrote:
    > In other words
    >
    > Array<int,1> A(5), B(10);
    > A = B(Range(0,4)); // Statement 1
    > Array<int,1> C = B(Range(0,4)); // Statement 2


    Here operator= is *not* used. It's called _initialisation_ and *not*
    assignment. Overloading the assignment operator won't help you. You
    need a proper constructor for that.

    >
    > I d like
    > Statement 1 results in a portion of B's data being copied into A
    > and
    > After Statement 2 is executed, the array C is a reference (or alias) to
    > B's data.


    V
     
    Victor Bazarov, Jun 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,902
    Tris Orendorff
    Apr 29, 2004
  2. Tony Johansson

    deep and shallow copy

    Tony Johansson, May 19, 2005, in forum: C++
    Replies:
    5
    Views:
    9,832
    Rapscallion
    May 19, 2005
  3. Alex
    Replies:
    2
    Views:
    1,237
  4. Replies:
    26
    Views:
    2,121
    Roland Pibinger
    Sep 1, 2006
  5. shuisheng
    Replies:
    4
    Views:
    839
    Uenal S. Mutlu
    Dec 17, 2006
Loading...

Share This Page