• Source
    1. #include <iostream>
    2. #include <memory>
    3. #include <vector>
    4. using namespace std;
    5.  
    6. class Hoge {
    7. public:
    8. Hoge() { cout << "Create Hoge!" << endl; }
    9. ~Hoge() { cout << "Delete Hoge!" << endl; }
    10.  
    11. shared_ptr<Hoge> next;
    12. };
    13.  
    14. int main() {
    15. shared_ptr<Hoge> hoge1(new Hoge());
    16. shared_ptr<Hoge> hoge2(new Hoge());
    17.  
    18. // 循環参照状態にする
    19. hoge1->next = hoge2;
    20. hoge2->next = hoge1;
    21.  
    22. // デストラクタが呼ばれない!!!
    23. // (循環参照しているため、まだ所有権を持っている人がいると思ってしまう)
    24. return 0;
    25. }
    26.