好.我们知道以下代码无法编译.
char source[1024];
char dest[1024];
// Fail. Use memcpy(dest, source, sizeof(source)); instead.
dest = source;
Run Code Online (Sandbox Code Playgroud)
但是,以下代码可以编译并且行为正确.
class A {
char data[1024];
};
A source;
B dest;
dest = source;
Run Code Online (Sandbox Code Playgroud)
我想知道,在运算符赋值函数中,数组将是隐式的memcpy吗?
以下是完整的测试代码.
#include <cstdio>
#include <memory>
class A {
public:
char data[1024];
};
int main() {
{
A source;
A dest;
// Initialization
char *data = "hello world";
memcpy (source.data, data, strlen(data) + 1);
printf ("source.data = %s\n", source.data);
printf ("address source.data = %x\n", source.data);
// Works! Does this in the operator assignment function, memcpy is
// being performed implicitly on array.
dest = source;
printf ("dest.data = %s\n", dest.data);
printf ("address dest.data = %x\n", dest.data);
}
{
char source[1024];
char dest[1024];
// Initialization
char *data = "hello world";
memcpy (source, data, strlen(data) + 1);
printf ("source = %s\n", source);
printf ("address source = %x\n", source);
// '=' : left operand must be l-value
// dest = source;
// Works with memcpy.
memcpy(dest, source, sizeof(source));
printf ("dest = %s\n", dest);
printf ("address dest = %x\n", dest);
}
getchar();
}
//RESULT :
//source.data = hello world
//address source.data = 12fb60
//dest.data = hello world
//address dest.data = 12f758
//source = hello world
//address source = 12f344
//dest = hello world
//address dest = 12ef3c
Run Code Online (Sandbox Code Playgroud)
这引用标准帮助吗?这是安静的自我解释
抱歉,删除了我之前的答案,该答案与复制构造函数有关,而不是复制赋值运算符.
$ 12.8/30-
非联合类X的隐式定义的复制赋值运算符执行其子对象的成员复制赋值.首先按照它们在base-specifier-list中的声明顺序分配X的直接基类,然后按照它们在类定义中声明的顺序分配X的直接非静态数据成员. .每个子对象都以适合其类型的方式分配:
- 如果子对象是类类型,则使用该类的复制赋值运算符(就像通过显式限定;即忽略更多派生类中的任何可能的虚拟覆盖函数);
- 如果子对象是一个数组,则以适合于元素类型的方式分配每个元素;
- 如果子对象是标量类型,则使用内置赋值运算符.
如果没有为子元素找到复制向量/赋值操作,编译器生成的复制向量/赋值操作是按位复制。
编辑:
这是显示概念的修改后的测试用例。
#include <cstdio>
#include <memory>
class someElement
{
public:
someElement() : theData(0) {}
// Intentionally copy-edit
someElement(const someElement& src) : theData(src.theData + 1) {}
~someElement(){}
someElement& operator=(const someElement& rhs)
{
theData = rhs.theData - 1;
return *this;
}
char theData;
};
class A {
public:
someElement data[1024];
};
int main() {
{
A source;
A dest;
// Initialization
char *data = "hello world";
memcpy (source.data, data, strlen(data) + 1);
printf ("source.data = %s\n", source.data);
printf ("address source.data = %x\n", source.data);
// Works! Does this in the operator assignment function, memcpy is
// being performed implicitly on array.
dest = source;
printf ("dest.data = %s\n", dest.data);
printf ("address dest.data = %x\n", dest.data);
}
{
someElement source[1024];
someElement dest[1024];
// Initialization
char *data = "hello world";
memcpy (source, data, strlen(data) + 1);
printf ("source = %s\n", source);
printf ("address source = %x\n", source);
// '=' : left operand must be l-value
// dest = source;
// Works with memcpy.
memcpy(dest, source, sizeof(source));
printf ("dest = %s\n", dest);
printf ("address dest = %x\n", dest);
}
getchar();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1623 次 |
| 最近记录: |