Thread: Copy object

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Copy object

    If I have this
    Code:
    class A
    {
       int a;
       Object B;
       ....
    };
    
    class B
    {
       int b;
       AnotherObj C;
    };
    ...
    and I do
    Code:
    A a = myObjA;
    then the copy constructor is called and a shallow copy is made. Is a.B copied as well?

    I know that a shallow copy in languages like C# or Java don't copy the objects, since they only have references. But what about in C++?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Each member is copied.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tabstop View Post
    Each member is copied.
    While true, I think that's a bit incomplete.

    Each member is "copied," but what that actually means depends on the type of member.

    Objects held by value are copied, meaning a new instance is created using the copy constructor.

    Pointers are copied, meaning a new pointer is created but the underlying object is not altered or copied.

    References are copied, meaning a new reference is created but again, the underlying object is not altered or copied.

    Generally, if the member objects themselves do "the right thing" when copied, then a class containing these objects will also do the right thing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by C_ntua View Post
    If I have this
    Code:
    class A
    {
       int a;
       Object B;
       ....
    };
    
    class B
    {
       int b;
       AnotherObj C;
    };
    ...
    and I do
    Code:
    A a = myObjA;
    then the copy constructor is called and a shallow copy is made. Is a.B copied as well?

    I know that a shallow copy in languages like C# or Java don't copy the objects, since they only have references. But what about in C++?
    A shallow copy has the same meaning in all languages. The question here is whether this is a shallow copy or not. The answer to that depends on the behaviour of AnotherObj. I.e. whether it is ref-counted, CoW, or just plain duplicated in the copy-constructor etc.
    If AnotherObj's only member were an int for example, then here a shallow copy and a deep copy are the same thing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy HWND into shared memory buffer
    By Opariti in forum Windows Programming
    Replies: 2
    Last Post: 12-26-2009, 01:08 PM
  2. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  3. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  4. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM