オブジェクトのディープ コピー (コピー オン ライト用) を作成しようとしていますが、セグメンテーション エラーが発生します。
リンクリストでハッシュテーブルを使用しています。
class Person
{
public:
Person(const char * id,int nb)
{
this->id=strdup(id);
this->nb=nb;
this->init=init;
this->next=NULL;
}
Person(const Person& rhs) :
nb(rhs.nb),
init(rhs.init),
id(strdup(rhs.id)),
next(rhs.next == NULL ? NULL : new Person(*rhs.next)) {}
char* strdup(char const* in)
{
char* ret = new char[strlen(in)+1];
strcpy(ret, in);
return ret;
}
int nb,init;
const char * id;
Person *next;
};
Hashtable deepcopy (const Hashtable& rhs)
{
num[0]=num[0]-1;
Person** array=rhs.table;
Hashtable autre;
for (int i = 0 ; i < size; ++i)
if (autre.table[i]!=NULL)
autre.table[i] = new Person(*array[i]);
return autre;
num[0]=1;
}
私のクラス Hashtable の属性:
Person **table;
int* num;
編集: この問題は修正されたようです。 ディープ コピーの何が問題になっていますか? 理解できない。私のコピー コンストラクターは優れていると思いますが、実行するとセグ フォールトが発生する理由がわかりません。