Namespaces
Variants

std::enable_shared_from_this<T>:: enable_shared_from_this

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
constexpr enable_shared_from_this ( ) noexcept ;
(1) (C++11 이후)
enable_shared_from_this ( const enable_shared_from_this & other ) noexcept ;
(2) (C++11 이후)

새로운 enable_shared_from_this 객체를 생성합니다. weak_this 값 초기화(value-initialized) 됩니다.

목차

매개변수

기타 - 복사할 enable_shared_from_this 객체

참고 사항

이동 생성자가 없습니다: enable_shared_from_this 에서 파생된 객체를 이동해도 해당 객체의 공유된 식별성은 전달되지 않습니다.

예제

#include <memory>
struct Foo : public std::enable_shared_from_this<Foo>
{
    Foo() {} // 암시적으로 enable_shared_from_this 생성자 호출
    std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
int main()
{
    std::shared_ptr<Foo> pf1(new Foo);
    auto pf2 = pf1->getFoo(); // pf1과 객체의 소유권을 공유
}

참고 항목

(C++11)
공유 객체 소유권 의미론을 가진 스마트 포인터
(클래스 템플릿)