我已經C++深複製嵌入了許多簡單的變量外加一個嵌套結構的結構。(memcpy的?)
struct Parent
{
int child1;
int child2;
char child3;
float child 4;
anotherStruct child5;
};
typedef struct
{
unsigned char x;
int y;
char z;
float a;
int b;
char c;
etc ..
} anotherStruct;
Parent myFirstParent;
Parent mySecondParent;
///I want to do a deep copy of myFirstParent into mySecondParent.
//does the follwowing work for that purpose??
memcpy (&mySecondParent, &myFirstParent, sizeof(myFirstParent);
我研究自己的答案,但同時我張貼,因爲極端的時間問題約束。提前致謝。
如果** Parent **結構有一個std :: map或std :: string作爲其成員之一,那麼mySecondParent = myFirstParent是否仍然有效? – Shyam 2016-03-01 22:53:57
@Shyam:是的。所有標準容器/類型都支持值語義,這意味着「分配」不會「深度複製」對象。 – Nawaz 2016-03-02 04:19:41