Search code examples
c++constructorcopyvariable-assignment

Does the implicit copy constructor copy array data members?


Possible Duplicate:
How are C array members handled in copy control functions?

I would guess the implicit copy constructor (generated by compiler) would copy pointer data members if they are declared as pointer.

I'm not sure what happens to array data members.

Does the implicit copy constructor copy array members correctly? What about the assignment operator?

For instance:

char mCharArray[100];
int mIntArray[100];   

Would the mCharArray and mIntArray be copied correctly?


Solution

  • Yes and yes is the answer. This is also true of structs in C.

    typedef struct {
        int a[100];
    } S;
    
    S s1;
    s1.a[0] = 42;
    S s2;
    s2 = s1;    // array copied