Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

C++ member access operators

Reference for C++ member access operators: the dot, arrow, pointer-to-member .* and ->*, overloading operator->, smart pointers and member pointers.

C++ has four operators for reaching into an object. The dot . selects a member of an object or reference. The arrow -> selects a member through a pointer: p->x is shorthand for (*p).x. The pointer-to-member operators .* and ->* apply a member pointer (a value that names a member without naming an object) to an object or a pointer.

-> can be overloaded, which is how smart pointers and iterators give you pointer-like syntax. The dot and .* cannot be overloaded.

C++
Point p{1, 2};
Point* q = &p;
p.x;      q->x;      (*q).x;

Try it

C++ Dot and arrow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Pointers to members and an overloaded arrow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
object.member            // member of an object or reference
pointer->member          // same as (*pointer).member
object.*member_ptr       // apply a pointer to member
pointer->*member_ptr     // same, through a pointer

T* T::operator->();      // overloadable (usually returns a raw pointer)
Name Description
. Selects a data member, member function, nested type or enumerator of an object. Not overloadable.
-> Selects a member through a pointer or an object with operator->.
.* Applies a pointer to member to an object. Not overloadable.
->* Applies a pointer to member to a pointer. Overloadable (rarely useful).

Returns: the member as an lvalue (data members) or a callable expression (member functions). For a member function pointer, the call needs parentheses: (obj.*fp)(args), because the call operator binds tighter than .*.

Errors / undefined behavior: -> on a null or dangling pointer is undefined behavior. Applying a member pointer to an object of an unrelated class is ill-formed, and to an object of the wrong dynamic type is undefined behavior.

Usage notes

  • Overloading ->: operator-> must return a raw pointer or another object that itself has operator->; the compiler keeps applying it until it reaches a raw pointer. This is called drill-down behavior.
  • No operator.: the language has no way to overload the dot (a proposal exists but has not been adopted), so wrapper classes cannot forward . to the wrapped object.
  • Member pointers are not addresses: &Account::fee is an offset-like value, not a pointer to an object. It cannot be converted to void* and its size can differ from an ordinary pointer.
  • Alternatives: std::function, lambdas, and std::invoke(&Account::total, acc) call member pointers with ordinary function syntax. std::invoke also works for plain functions, so it is the better choice in generic code.
  • Dependent names: inside templates you may need p->template foo<int>() when foo is a member template of a dependent type.
  • Smart pointers: std::unique_ptr and std::shared_ptr provide -> and *; the pointee's members are reached with ->, the smart pointer's own members (reset(), get()) with ..
  • Optional and variants: std::optional<T> also has ->; value() checks for emptiness while -> does not.
  • Static members: a static member can be named with . or -> on an object, but Class::member is clearer (Scope resolution).