c++ 是否隐式复制构造函数复制数组成员变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5700204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ does implicit copy constructor copy array member variable?
提问by eugene
Possible Duplicate:
How are C array members handled in copy control functions?
可能的重复:
如何在复制控制函数中处理 C 数组成员?
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?
mCharArray mIntArray 会被正确复制吗?
回答by
Yes and yes is the answer. This is also true of structs in C.
是的,是的就是答案。C 中的结构体也是如此。
typedef struct {
int a[100];
} S;
S s1;
s1.a[0] = 42;
S s2;
s2 = s1; // array copied
回答by Tony Delroy
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.
在这里,复制构造函数X b = a
更安全、更直观,因为它自己制作所有字符串数据的副本,并且不再依赖或连接到复制的X
对象,但是这更慢并且可能更浪费内存。
回答by Sujay Ghosh
"implicit copy constructor(generated by compiler)" - does a shallow copy for all variables.
“隐式复制构造函数(由编译器生成)” - 对所有变量进行浅拷贝。
回答by iammilind
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).
是的。C/C++ 中内置了复制构造函数和赋值运算符。它们逐字节复制(这对较大的数组不利,因为它会导致代码膨胀)。它还复制指针,但那将是浅拷贝(如果指针指向某个位置,则复制的指针也将指向同一位置)。