次に例を示します。
template<typename T>
struct smart { //Smart Pointer class
smart();
~smart();
smart(const smart& copy);
T* target;
int count;
};
struct atest {
smart<atest> next;
};
void Garbage() {
smart_ptr<atest> Test=smart<atest>(new atest);
//Test.count == 1
Test->next=Test;
//Test.count == 2
//Test.target == Test->next.target
}
//Test.count == 1
//Test'll never be deleted! because it contains itself.
int main() {
for (int i=0;i<10000000;i++) {
Garbage();
}
}
メソッドの終了Test
後に自分自身を削除するという解決策はありますか? Garbage
そして、ここで別の質問です。スマート ポインターには別の穴がありますか?