What three steps in overloading the arrow operator?

F

fl

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,
 
B

Bart van Ingen Schenau

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top