隐式复制构造函数是否复制数组数据成员?

发布于 2024-11-02 04:18:16 字数 580 浏览 4 评论 0原文

可能的重复:
复制控制函数中如何处理 C 数组成员?< /a>

我猜测隐式复制构造函数(由编译器生成)会复制指针数据成员(如果它们被声明为指针)。

我不确定数组数据成员会发生什么。

隐式复制构造函数是否正确复制数组成员?那么赋值运算符呢?

例如:

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

mCharArraymIntArray 能否正确复制?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-11-09 04:18:16

是的,是的,这就是答案。 C 中的结构体也是如此。

typedef struct {
    int a[100];
} S;

S s1;
s1.a[0] = 42;
S s2;
s2 = s1;    // array copied

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
短叹 2024-11-09 04:18:16

只是为了尽可能清楚地说明:

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'

但是,潜在的令人讨厌的情况是针对指针和引用的:

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!

希望这有助于说明问题。

考虑一种使结构更加“复制安全”的方法:

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];
};

这里,复制构造函数使 X b = a 更安全、更直观,因为它为所有字符串数据创建了自己的副本,并且没有进一步依赖或连接到复制的 X 对象,但这速度较慢并且可能更浪费内存。

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.

孤君无依 2024-11-09 04:18:16

“隐式复制构造函数(由编译器生成)” - 对所有变量进行浅复制。

"implicit copy constructor(generated by compiler)" - does a shallow copy for all variables.

無處可尋 2024-11-09 04:18:16

是的。 C/C++ 中内置提供了复制构造函数和赋值运算符。它们进行逐字节复制(这对于较大的数组不利,因为它会导致代码膨胀)。它还复制指​​针,但这将是浅复制(如果指针指向某个位置,则复制的指针也将指向同一位置)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文