6
如果我有指針 - 複製對象實例
Class *a1 = new Class();
Class *b1 = a1;
delete b1;
delete a1; //this will give a double free or corruption message;
如果我刪除指針B,這是一樣的刪除指針吧?因爲兩者都指向Class的同一個實例。那麼,如何將a1的實例複製到b1,以便在刪除b1時不刪除a1。
Class *a1 = new Class();
Class *b1 = a1;
//how do I duplicate the instance a1 is pointing
//so that when I delete b1, a1 still exists.
delete b1;
delete a1; //this is still valid
謝謝。
不確定所有的指針是否必要,但'Class * b1 = new Class(* a1);'應該這樣做。 – chris
確保您的拷貝構造函數正常,並從* a1創建新副本。 – halfelf
這很容易。@謝謝 – tambalolo