• 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. weak_ptr<Hoge> next; // weak_ptr を使用
    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.