Top

shared_ptr

<memory> 를 포함해야 한다:

#include <memory>

shared_ptr 만들기:

auto song1 = make_shared<Song>(...);
auto song2 = make_shared<Song>(...);

소유권 공유:

auto song3 = song1;

객체 멤버 참조:

wcout << song3->artist << endl;

shared_ptr 멤버 참조:

if (song3.get() != nullptr) {}

weak_ptr

shared_ptr의 참조 카운트를 증가시키지 않는다.

<memory> 를 포함해야 한다:

#include <memory>

참조:

std::weak_ptr<int> wsong1(song1);
std::weak_ptr<int> wsong2(song2);

사용 전에 반드시 만료 되었는지 확인:

if (!wsong1.expired()) {
    ...
}

두 객체 교환:

wsong1.swap(wsong2);

소유 리소스 해제:

wsong1.reset();

참조

참조