std::shared_ptr<T>:: get
From cppreference.net
<
cpp
|
memory
|
shared ptr
C++
Memory management library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::shared_ptr
| Member functions | ||||
| Modifiers | ||||
| Observers | ||||
|
shared_ptr::get
|
||||
|
(C++17)
|
||||
|
(
until C++20*
)
|
||||
|
(C++26)
|
||||
|
(C++26)
|
||||
| Non-member functions | ||||
|
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(until C++20)
(C++20)
|
||||
|
functions
(
until C++26*
)
|
||||
| Helper classes | ||||
|
(C++20)
|
||||
| Deduction guides (C++17) |
|
T
*
get
(
)
const
noexcept
;
|
(C++17 이전) | |
|
element_type
*
get
(
)
const
noexcept
;
|
(C++17 이후) | |
저장된 포인터를 반환합니다.
목차 |
매개변수
(없음)
반환값
저장된 포인터.
참고 사항
A
shared_ptr
는 객체의 소유권을 공유하면서 다른 객체를 가리키는 포인터를 저장할 수 있습니다.
get()
는 관리되는 포인터가 아닌 저장된 포인터를 반환합니다.
예제
이 코드 실행
#include <iostream> #include <memory> #include <string_view> int main() { auto output = [](std::string_view msg, int const* pInt) { std::cout << msg << *pInt << " in " << pInt << '\n'; }; int* pInt = new int(42); std::shared_ptr<int> pShared = std::make_shared<int>(42); output("Naked pointer: ", pInt); // output("Shared pointer: ", pShared); // compiler error output("Shared pointer: ", &*pShared); // OK, calls operator*, then takes addr output("Shared pointer with get(): ", pShared.get()); delete pInt; std::cout << "\nThe shared_ptr's aliasing constructor demo.\n"; struct Base1 { int i1{}; }; struct Base2 { int i2{}; }; struct Derived : Base1, Base2 { int i3{}; }; std::shared_ptr<Derived> p(new Derived()); std::shared_ptr<Base2> q(p, static_cast<Base2*>(p.get())); std::cout << "q shares ownership with p, but points to Base2 subobject:\n" << "p.get(): " << p.get() << '\n' << "q.get(): " << q.get() << '\n' << "&(p->i1): " << &(p->i1) << '\n' << "&(p->i2): " << &(p->i2) << '\n' << "&(p->i3): " << &(p->i3) << '\n' << "&(q->i2): " << &(q->i2) << '\n'; }
가능한 출력:
Naked pointer: 42 in 0xacac20 Shared pointer: 42 in 0xacac50 Shared pointer with get(): 42 in 0xacac50 The shared_ptr's aliasing constructor demo. q shares ownership with p, but points to Base2 subobject: p.get(): 0xacac20 q.get(): 0xacac24 &(p->i1): 0xacac20 &(p->i2): 0xacac24 &(p->i3): 0xacac28 &(q->i2): 0xacac24
참고 항목
|
저장된 포인터를 역참조합니다
(public member function) |