Namespaces
Variants
Views
Actions

std::unique_ptr<T,Deleter>::operator*, std::unique_ptr<T,Deleter>::operator->

From cppreference.com
< cpp‎ | memory‎ | unique ptr
 
 
Utilities library
General utilities
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)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
Dynamic memory management
Smart pointers
(C++11)
(C++11)
(C++11)
(until C++17)
(C++11)
Allocators
Memory resources
Uninitialized storage
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Garbage collection support
Miscellaneous
(C++20)
(C++11)
(C++11)
C Library
Low level memory management
 
 
typename std::add_lvalue_reference<T>::type operator*() const;
(1) (since C++11)
pointer operator->() const noexcept;
(2) (since C++11)

operator* and operator-> provide access to the object owned by *this.

The behavior is undefined if get() == nullptr.

These member functions are only provided for unique_ptr for the single objects i.e. the primary template.

Contents

[edit] Parameters

(none)

[edit] Return value

1) Returns the object owned by *this, equivalent to *get().
2) Returns a pointer to the object owned by *this, i.e. get().

[edit] Exceptions

1) may throw, e.g. if pointer defines a throwing operator*

[edit] Example

#include <iostream>
#include <memory>
 
struct Foo {
    void bar() { std::cout << "Foo::bar\n"; }
};
 
void f(const Foo& foo) 
{
    std::cout << "f(const Foo&)\n";
}
 
int main() 
{
    std::unique_ptr<Foo> ptr(new Foo);
 
    ptr->bar();
    f(*ptr);
}

Output:

Foo::bar
f(const Foo&)

[edit] See also

returns a pointer to the managed object
(public member function) [edit]