public member function
<memory>

std::unique_ptr::operator*

typename add_lvalue_reference<element_type>::type operator*() const;
Dereference object
Returns a reference to the managed object.

The unique_ptr shall not be empty (i.e., its stored pointer shall not be a null pointer) in order to be dereferenciable. This can easily be checked by casting the unique_ptr object to bool (see unique_ptr::operator bool).

It is equivalent to: *get().

This member function is exclusive of the non-specialized version of unique_ptr (for single objects). The array specialization does not include it.

Parameters

none

Return value

A reference to the object pointed.
element_type is a member type, defined as an alias of unique_ptr's first template parameter.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// unique_ptr::operator*
#include <iostream>
#include <memory>

int main () {
  std::unique_ptr<int> foo (new int);
  std::unique_ptr<int> bar (new int (100));

  *foo = *bar * 2;

  std::cout << "foo: " << *foo << '\n';
  std::cout << "bar: " << *bar << '\n';

  return 0;
}

Output:
foo: 200
bar: 100


See also