std::shared_ptr::operator*, std::shared_ptr::operator-> - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::shared_ptr::operator*, std::shared_ptr::operator->

From cppreference.com
< cpp‎ | memory‎ | shared ptr

T& operator*() const; | (1) | (since C++11)
T* operator->() const; | (2) | (since C++11)

Dereferences pointer to the managed object.

### [edit] Parameters

(none)

### [edit] Return value

1) reference to the managed object.
2) pointer to the managed object.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] Notes

When `element_type` is `void`, it is unspecified whether function (1) is declared. If it is declared, 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>.

### [edit] Example

Run this code

    #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

### [edit] See also

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/operator*&oldid=80265"
Category:

- unconditionally noexcept