W3cubDocs

/C++

std::unique_ptr::operator*

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.

Parameters

(none).

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().

Exceptions

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

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&)

See also

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

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/memory/unique_ptr/operator*