[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/memory/shared_ptr/~shared_ptr [Back]  [Original]

std::shared_ptr::~shared_ptr cppreference.com
cppreference.com

std::shared_ptr::~shared_ptr

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
no section name
no section name
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)



no section name
 
 
<tbody> </tbody>
~shared_ptr();

*this , shared_ptr, , .

, *this, , use_count(), , .

std::unique_ptr, std::shared_ptr , .

#include <memory>
#include <iostream>

struct S {
    S() { std::cout << "S::S()\n"; }
    ~S() { std::cout << "S::~S()\n"; }
    struct Deleter {
        void operator()(S* s) const {
            std::cout << "S::Deleter()\n";
            delete s;
        }
    };
};

int main()
{
    auto sp = std::shared_ptr<S>{ new S, S::Deleter{} };

    auto use_count = [&sp](char c) {
        std::cout << c << ") use_count(): " << sp.use_count() << '\n';
    };

    use_count('A');
    {
        auto sp2 = sp;
        use_count('B');
        {
            auto sp3 = sp;
            use_count('C');
        }
        use_count('D');
    }
    use_count('E');

//  sp.reset();
//  use_count('F'); //  "F) use_count(): 0"
}

:

S::S()
A) use_count(): 1
B) use_count(): 2
C) use_count(): 3
D) use_count(): 2
E) use_count(): 1
S::Deleter()
S::~S()

weak_ptr
(public - std::weak_ptr<T>) []

Web Proxy Viewer  |  New URL  |  Original Page