What three steps in overloading the arrow operator?

Discussion in 'C++' started by fl, Jan 27, 2013.

  1. fl

    fl Guest

    Hi,
    I read "C++ Primer", 4th edition. On Chapter 14, page 525, it talks about overloading the arrow operator. I do not understand "then repeat these threesteps". I have check the errata, there is no mistake in this page. These is a link which also mentions steps (but it is still not answers to my questions)

    http://stackoverflow.com/questions/10677804/how-arrow-operator-overloading-works-internally-in-c

    I rewrite the text here in order the reader can help me.

    "When we write

    point->action();

    precedence rules make it equivalent to writing

    (point->action)();

    In other words, we want to call the result of evaluating point->action. Thecompiler evaluates this code as follows:

    1. If point is a pointer to a class object that has a member named action, then the compiler writes code to call the action member of that object.

    2. Otherwise, if point is an object of a class that defines operator->, then point->action is the same as point.operator->()->action, That is, we execute operator->() on point and then repeat these three steps, using the result of executing operator-> on point.

    3. Otherwise, the code is in error."

    I do not know the last part of item 2. I do not find any snippet code using
    ->display. I do not find any part mentioning 3 steps. Thus, I cannot solve this myself.
    Could you help me out? Thanks,
     
    fl, Jan 27, 2013
    #1
    1. Advertising

  2. On Sun, 27 Jan 2013 13:25:54 -0800, fl wrote:

    <snip>
    > In other words, we want to call the result of evaluating point->action.
    > The compiler evaluates this code as follows:
    >
    > 1. If point is a pointer to a class object that has a member named
    > action, then the compiler writes code to call the action member of that
    > object.
    >
    > 2. Otherwise, if point is an object of a class that defines operator->,
    > then point->action is the same as point.operator->()->action, That is,
    > we execute operator->() on point and then repeat these three steps,
    > using the result of executing operator-> on point.
    >
    > 3. Otherwise, the code is in error."
    >
    > I do not know the last part of item 2. I do not find any snippet code
    > using ->display. I do not find any part mentioning 3 steps. Thus, I
    > cannot solve this myself. Could you help me out? Thanks,


    The three steps mentioned in item 2 are the three numbered items I quoted
    above.
    The operator-> is special, because it's application can be repeated
    without this being directly observable in the code.
    For example:

    #include <iostream>

    struct Foo {
    void print() { std::cout << "Hello from Foo" << std::endl; }
    };

    struct Bar {
    Foo mFoo;
    Foo* operator->() { return &mFoo; }
    };

    struct Baz {
    Bar mBar;
    Bar& operator->() { return mBar; }
    };

    int main()
    {
    Foo aFoo, *pFoo = &aFoo;
    Bar aBar;
    Baz aBaz;

    pFoo->print(); /* Item 1: print is a member of Foo */
    aBar->print(); /* Item 2: use operator-> */
    aBaz->print(); /* Item 2 repeated: use operator-> twice */
    }

    This code compiles and prints "Hello from Foo" three times.

    Bart v Ingen Schenau
     
    Bart van Ingen Schenau, Jan 28, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    449
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    768
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    756
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    585
  5. Replies:
    2
    Views:
    336
Loading...

Share This Page