问题
Possible Duplicate:
How are C array members handled in copy control functions?
I would guess implicit copy constructor (generated by compiler) would copy pointer if member variable is declared as pointer.
I'm not sure what happens to array member variable.
Does implicit copy constructor copy array member correctly? How about assignment operator?
For instance:
char mCharArray[100];
int mIntArray[100];
Would the mCharArray mIntArray be copied correctly?
回答1:
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
回答2:
Just to make it as clear as possible:
struct X
{
char data_[100];
};
X a, b;
a.data_[10] = 'x';
b = a;
// here, b.data_[n] == a.data_[n] for 0 <= n < 100, so b.data_[10] == 'x'
BUT, the potentially nasty case is for pointers and references:
struct X
{
char* data_[100];
};
X a, b;
a.data_[10] = new char[6]; // a character array on the heap
strcpy(a.data_[10], "hello"); // put some text into it...
b = a;
// here, b.data_[n] == a.data_[n] for 0 <= n < 100
// so b.data_[10] == a.data_[10] == same character array containing "hello"
// BUT...
b.data_[10][2] = 'L'; // change text to "heLlo" via b.data_[10] pointer...
// here, a.data_[10][2] will be 'L' too, as a.data_[10] and b.data_[10] both point
// to the same underlying heap memory returned by new above...
delete[] a.data_[10]; // ok...
std::cout << b.data_[10]; // NOT ok - this memory's been deallocated!
delete[] b.data_[10]; // NOT ok - this memory's (already) been deallocated!
Hopefully that helps illustate the issue.
Consider one way to make the structure more "copy-safe":
struct X
{
X(const X& rhs)
{
for (int i = 0; i < 100; ++i)
if (rhs.data_[i])
{
// deep copy of pointed-to text...
data_[i] = new char[strlen(rhs.data_[i]) + 1];
strcpy(data_[i], rhs.data_[i]);
}
else
data_[i] = NULL;
}
char* data_[100];
};
Here, the copy-constructor makes X b = a
safer and more intuitive because it makes its own copy of all the string data and has no further dependency on or connection to the copied X
object, but this is slower and potentially more wasteful of memory.
回答3:
"implicit copy constructor(generated by compiler)" - does a shallow copy for all variables.
回答4:
Yes. Copy constructor and Assignment operators are in-built provided in C/C++. They do byte by byte copy (which is not good for larger arrays, as it will cause code bloat). It also copies pointer but that will be shallow copy (if pointer is pointing to some location, the copied pointer will also point to same location).
来源:https://stackoverflow.com/questions/5700204/c-does-implicit-copy-constructor-copy-array-member-variable