std::shared_ptr<T>::get
提供: cppreference.com
< cpp | memory | shared ptr
| 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
[編集] 関連項目
| 格納されているポインタを逆参照します (パブリックメンバ関数) |

