Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What three steps in overloading the arrow operator?

Reply
Thread Tools

What three steps in overloading the arrow operator?

 
 
fl
Guest
Posts: n/a
 
      01-27-2013
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/1...nternally-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,


 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      01-28-2013
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Three steps to convert video and audio files in all formats MichelleRuby Software 2 02-03-2011 06:45 PM
Re: Three Mobile --> Skype on three (Non-three [Symbian - Nokia] handsets) Harry Stottle UK VOIP 0 01-05-2010 08:59 AM
Three Steps for the Backup of the Film yutang1210 Software 0 05-08-2008 05:41 AM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments