Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4034266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:57:03+00:00 2026-05-20T11:57:03+00:00

I saw the following example in book of the C++ Programming Language class Ptr

  • 0

I saw the following example in book of the C++ Programming Language

class Ptr {
     X* operator->( );
};

 voide f(Ptr p)
 {
   p->m=7;
   X* q1 = p->;
   X* q2 = p.operator->();
 }

The book claims that
1)Objects of class Ptr can be used to access members of class X in a very similar manner to the way pointers are used.
2)The transformation of the object p into the pointer p.operator->() does not depend on the member m pointed to. That is the sense in which operator->( ) is a unary postfix operator.

For the first point, I do not understand why we need to this design, or in which scenarios should use this kind of design.
For the second point, I am confused about the message that the author want to deliver.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:57:03+00:00Added an answer on May 20, 2026 at 11:57 am
    1. This design is extremely useful when we want to create a class that behaves like a pointer; this is the case of smart pointers (objects that have a pointer-like interface but that provide additional "services", e.g. automatic deallocation on destruction, ownership transfer, reference counting, …).

      Notice that often this kind of object will also overload the * (dereference) operator to mimic pointers more closely.

    2. The author wants to say that when you use the -> operator on Ptr, it’s not relevant (as far as operator-> is concerned) what you put after it: in any case, the operator-> method will be called, that will return a pointer to the object that will be considered for the rest of the expression.


    To make this more clear: quoting directly from the standard:

    13.5.6 Class member access

    operator-> shall be a non-static member function taking no parameters. It implements class member access using ->

    postfix-expression -> id-expression
    

    An expression x->m is interpreted as (x.operator->())->m for a class object x of type T if T::operator->() exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3).

    In other words:

    • if you call operator->() directly (second and third example in the OP code), you will get the pointer returned by the operator-> method, just like what happens with any method;
    • if you put stuff after the -> operator (e.g. x->m, as in the first example in the OP code), the overloaded operator-> will be called, and the returned pointer will be used as if the ->m was being used over it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was following an example in my book for an AddressCard class. One of
I saw the following fragment of C code in a text book and it's
I have the following html code: I saw that for Watir-webdriver the Watir::Image.file_size method
I saw some websites that (for example): if you want to view your message
I saw the following example disccussed here previously, where the goal was to return
I saw the following example on Nabble , where the goal was to return
I saw the following example: Embedding a matplotlib figure inside a WxPython panel I
I saw the following function call in Yacfe example : Visitor_c.vk_program { Visitor_c.default_visitor_c with
Is it a pretty safe assumption that the following class is an odd representation
I am following few tutorials for creating MVC3 application. I saw example of adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.