std::shared_ptr<T>::get
提供: cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev ">
</tbody><tbody>
</tbody>
T* get() const noexcept; |
(C++17未満) | |
element_type* get() const noexcept; |
(C++17以上) | |
格納されているポインタを返します。
引数
(なし)
戻り値
格納されているポインタ。
ノート
shared_ptr は所有権を共有しているオブジェクトとは別のオブジェクトを指すポインタを格納している場合があります。 get() は管理対象のポインタではなく、格納されているポインタを返します。
例
Run this code
#include <iostream>
#include <memory>
#include <string_view>
void output(std::string_view msg, int const* pInt)
{
std::cout << msg << *pInt << "\n";
}
int main()
{
int* pInt = new int(42);
std::shared_ptr<int> pShared = std::make_shared<int>(42);
output("Naked pointer ", pInt);
// output("Shared pointer ", pShared); // コンパイルエラー
output("Shared pointer with get() ", pShared.get());
delete pInt;
}
出力:
Naked pointer 42
Shared pointer with get() 42
関連項目
| 格納されているポインタを逆参照します (パブリックメンバ関数) |