Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shared_ptr<T>::get

      From cppreference.com
      <cpp‎ |memory‎ |shared ptr
       
       
      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*)
      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)
       
      std::shared_ptr
      Member functions
      Modifiers
      Observers
      shared_ptr::get
      (until C++20*)
      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
      Deduction guides(C++17)
       
      T* get()constnoexcept;
      (until C++17)
      element_type* get()constnoexcept;
      (since C++17)

      Returns the stored pointer.

      Contents

      [edit]Parameters

      (none)

      [edit]Return value

      The stored pointer.

      [edit]Notes

      Ashared_ptr may share ownership of an object while storing a pointer to another object.get() returns the stored pointer, not the managed pointer.

      [edit]Example

      Run this code
      #include <iostream>#include <memory>#include <string_view> int main(){auto output=[](std::string_view msg,intconst* pInt){std::cout<< msg<<*pInt<<" in "<< pInt<<'\n';}; int* pInt= newint(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';}

      Possible output:

      Naked pointer: 42 in 0xacac20Shared pointer: 42 in 0xacac50Shared 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(): 0xacac20q.get(): 0xacac24&(p->i1): 0xacac20&(p->i2): 0xacac24&(p->i3): 0xacac28&(q->i2): 0xacac24

      [edit]See also

      dereferences the stored pointer
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/get&oldid=153753"

      [8]ページ先頭

      ©2009-2025 Movatter.jp