名前空間
変種
操作

std::enable_shared_from_this

提供: cppreference.com
< cpp‎ | memory

 
 
ユーティリティライブラリ
支援を入力します (basic types, RTTI, type traits)
動的なメモリ管理
エラー処理
プログラムユーティリティ
可変個引数関数
日付と時刻
関数オブジェクト
initializer_list(C++11)
bitset
hash(C++11)
関係演算子
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
ペアとタプル
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
フォワード、スワップ、および移動
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
動的なメモリ管理
低レベルのメモリ管理
アロケータ
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
allocator
allocator_traits(C++11)
allocator_arg_t(C++11)
allocator_arg(C++11)
uses_allocator(C++11)
scoped_allocator_adaptor(C++11)
ストレージを初期化されていない
Original:
Uninitialized storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
uninitialized_copy
uninitialized_copy_n(C++11)
uninitialized_fill
uninitialized_fill_n
raw_storage_iterator
get_temporary_buffer
return_temporary_buffer
スマートポインタ
Original:
Smart pointers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(C++11)
shared_ptr(C++11)
weak_ptr(C++11)
auto_ptr(廃止予定)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
ガベージコレクションをサポートしています
Original:
Garbage collection support
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declare_reachable(C++11)
undeclare_reachable(C++11)
declare_no_pointers(C++11)
undeclare_no_pointers(C++11)
pointer_safety(C++11)
get_pointer_safety(C++11)
その他
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pointer_traits(C++11)
addressof(C++11)
align(C++11)
Cライブラリ
Original:
C Library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
std::enable_shared_from_this
メンバ関数を保護しました
Original:
Protected member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
enable_shared_from_this::enable_shared_from_this
enable_shared_from_this::~enable_shared_from_this
enable_shared_from_this::operator=
パブリックメンバ関数
Original:
Public member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
enable_shared_from_this::shared_from_this
 
Defined in header <memory>
template< class T > class enable_shared_from_this;
(C + + 11以来)
std::enable_shared_from_this現在安全tstd::shared_ptrのすべての所有権を共有してそのptで追加std::shared_ptrインスタンスを生成するためにpt1, pt2, ...名前tによって管理されているオブジェクトptができます.
Original:
std::enable_shared_from_this allows an object t that is currently managed by a std::shared_ptr named pt to safely generate additional std::shared_ptr instances pt1, pt2, ... that all share ownership of t with pt.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::enable_shared_from_this<T>から継承するメンバ関数T持つ型shared_from_thisを提供しています。タイプtのオブジェクトTstd::shared_ptr<T>名前pt、その後T::shared_from_thisstd::shared_ptr<T>tの所有権を共有していることを新しいptを返します呼び出すことによって管理されている場合.
Original:
Inheriting from std::enable_shared_from_this<T> provides a type T with a member function shared_from_this. If an object t of type T is managed by a std::shared_ptr<T> named pt, then calling T::shared_from_this will return a new std::shared_ptr<T> that shares ownership of t with pt.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
前のオブジェクトshared_from_thistを呼び出すのと、std::shared_ptrを所有tが存在しなければならないことに注意してください.
Original:
Note that prior to calling shared_from_this on an object t, there must be a std::shared_ptr that owns t.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
またenable_shared_from_thisがお互いを認識していない複数のオーナーによって、複数回破壊防止std::shared_ptr<T>(this)する可能性が高くなりthis、次のような式に代わるものを提供することに注意してください.
Original:
Also note that enable_shared_from_this provides an alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of eachother.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集] メンバ関数

enabled_shared_from_thisオブジェクトを作成します
Original:
constructs an enabled_shared_from_this object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(メンバー関数を保護しました)
enable_shared_from_thisオブジェクトを破棄します
Original:
destroys an enable_shared_from_this object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(メンバー関数を保護しました)
thisへの参照を返します
Original:
returns a reference to this
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(メンバー関数を保護しました)
shared_ptrの所有権を共有*thisを返します
Original:
returns a shared_ptr which shares ownership of *this
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(パブリックメンバ関数)

[編集] ノート

enable_shared_from_thisための一般的な実装では、std::weak_ptrへの弱い参照は(例えばthisなど)を保持​​することです。 std::shared_ptrのコンストラクタではなく、ポインタが誰によって管理されていないと仮定するのは、既存のenable_shared_from_thissとstd::shared_ptrベースと株式所有の存在を検出することができます.
Original:
A common implementation for enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr can detect presence of a enable_shared_from_this base and share ownership with the existing std::shared_ptrs, instead of assuming the pointer is not managed by anyone else.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]

#include <memory>
#include <iostream>
 
struct Good: std::enable_shared_from_this<Good>
{
    std::shared_ptr<Good> getptr() {
        return shared_from_this();
    }
};
 
struct Bad {
    std::shared_ptr<Bad> getptr() {
        return std::shared_ptr<Bad>(this);
    }
    ~Bad() { std::cout << "Bad::~Bad() called\n"; }
};
 
int main()
{
    // Good: the two shared_ptr's share the same object
    std::shared_ptr<Good> gp1(new Good);
    std::shared_ptr<Good> gp2 = gp1->getptr();
    std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
 
    // Bad, each shared_ptr thinks it's the only owner of the object
    std::shared_ptr<Bad> bp1(new Bad);
    std::shared_ptr<Bad> bp2 = bp1->getptr();
    std::cout << "bp2.use_count() = " << bp2.use_count() << '\n';
} // UB: double-delete of Bad

Output:

gp2.use_count() = 2
bp2.use_count() = 1
Bad::~Bad() called
Bad::~Bad() called
*** glibc detected *** ./test: double free or corruption

[編集] も参照してください

smart pointer with shared object ownership semantics
(クラステンプレート) [edit]