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