Search code examples
c++operator-overloadingmember-access

What is the use of overloading the member access operator?


Possible Duplicate:
Operator overloading

The member access operator -> can be overloaded to return a pointer to a data member, or some other variable.

Where is this feature used ? What coding problems does it solve or alternately, what does it make easier ?


Solution

  • The member access operator is a somewhat odd creature: It is meant to return a pointer or a class with a member access operator overloaded. Once it reaches a pointer it just accesses the corresponding member. The primary use of overloading the member access operator are smart pointers, e.g., std::shared_ptr<T> and std::unique_ptr<T>. Without this operator you'd need to use something like

    sp.get()->member
    

    or

    (*sp).member
    

    instead of

    sp->member