OK We know that the following code cannot be compiled.
char source[1024];
char dest[1024];
// Fail. Use memcpy(dest, source, sizeof(source)); instead.
dest = source;
But the following code can be compiled and behave correctly.
class A {
char data[1024];
};
A source;
B dest;
dest = source;
I was wondering, in the function of assigning the operator the array will be memcpy implicitly?
Below is the full test code.
#include <cstdio>
#include <memory>
class A {
public:
char data[1024];
};
int main() {
{
A source;
A dest;
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);
dest = source;
printf ("dest.data = %s\n", dest.data);
printf ("address dest.data = %x\n", dest.data);
}
{
char source[1024];
char dest[1024];
char *data = "hello world";
memcpy (source, data, strlen(data) + 1);
printf ("source = %s\n", source);
printf ("address source = %x\n", source);
memcpy(dest, source, sizeof(source));
printf ("dest = %s\n", dest);
printf ("address dest = %x\n", dest);
}
getchar();
}
source
share