std::shared_ptr<T>::operator*, std::shared_ptr<T>::operator->

From cppreference.com
< cpp‎ | memory‎ | shared ptr
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
Dynamic memory management
Smart pointers
(C++11)
(C++11)
(C++11)
(until C++17)
(C++11)
(C++23)
Allocators
Memory resources
Uninitialized storage
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Garbage collection support
(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)
Miscellaneous
(C++20)
(C++11)
(C++11)
 
 
T& operator*() const noexcept;
(1) (since C++11)
T* operator->() const noexcept;
(2) (since C++11)

Dereferences the stored pointer. The behavior is undefined if the stored pointer is null.

Parameters

(none)

Return value

1) The result of dereferencing the stored pointer, i.e., *get()
2) The stored pointer, i.e., get()

Remarks

When T is a (possibly cv-qualified) void, it is unspecified whether function (1) is declared.

When T is an array type, it is unspecified whether these member functions are declared, and if they are, what their return type is, except that the declaration (not necessarily the definition) of these functions is well-formed. (since C++17)

If either function is declared despite being unspecified, it is unspecified what its return type is, except that the declaration (although not necessarily the definition) of the function is guaranteed to be legal. This makes it possible to instantiate std::shared_ptr<void>.

Example

#include <iostream>
#include <memory>
 
struct Foo
{
   Foo(int in) : a(in) {}
   void print() const
   {
      std::cout << "a = " << a << '\n';
   }
   int a;
};
 
int main()
{
   auto ptr = std::make_shared<Foo>(10);
   ptr->print();
   (*ptr).print();
}

Output:

a = 10
a = 10

See also

returns the stored pointer
(public member function)