Thread: Array members copied by the default copy constructors?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    Array members copied by the default copy constructors?

    Are array members copied "by content" by the default copy constructors? Or will I be modifying both "copies" if I modify one of the members like what would happen were they pointers to dynamically allocated memory?

    Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    pointers are copied
    memory they point - not
    so you get two instances pointing same dynamic memory

    It is a reason to use vectors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    pointers are copied
    memory they point - not
    so you get two instances pointing same dynamic memory

    It is a reason to use vectors
    Thanks for your reply. I know what would happen if I use dynamic memory. What about arrays allocated on the stack?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You mean like this:

    Code:
    class test
    {
       int a[10];
    };
    they'll be copied by value
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    okay thanks =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with copy constructors
    By arya6000 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2008, 01:34 AM
  2. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  3. FAST method to copy char array?
    By MitchellH in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2005, 08:19 AM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Half my function is lazy...
    By Shadow in forum C Programming
    Replies: 0
    Last Post: 10-09-2001, 09:46 AM