By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Is -> syntactic sugar?

P: n/a
Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually need
both? Say I write

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile? Does
the compiler translate it into this?

SharedPtr p;
(*p).bar;

Any help is appreciated.
Jul 22 '05 #1
Share this Question
Share on Google+
17 Replies


P: n/a
Of course it will compile... If you have a pointer to a class and want
to call one member function you use "->"... The class doesn't have to
implement it.

cmad
Jul 22 '05 #2

P: n/a
On Mon, 15 Dec 2003 20:39:26 +1100, "Jason Heyes" <ge******@optusnet.com.au> wrote:
Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually need
both? Say I write

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile?
You really should get yourself a C++ compiler.

Using Visual C++ 7.1:
error C2819: type 'SharedPtr' does not have an overloaded member 'operator ->'

Does the compiler translate it into this?

SharedPtr p;
(*p).bar;


No.

Just in case this is homework (I find it incredible that you don't have a
compiler) you'll have to look up the relevant paragraphs in the std yourself.

Jul 22 '05 #3

P: n/a
Chris Mantoulidis wrote in
news:a8**************************@posting.google.c om:
Of course it will compile... If you have a pointer to a class and want
to call one member function you use "->"... The class doesn't have to
implement it.

cmad
What you see above is called quoting, as long as it isn't over done
it helps people understand what you are talking about.

In this case: you seem to be responding to:
Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually
need both? Say I write
You need to implement operator ->() if you want to use it.

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile?
Does the compiler translate it into this?

SharedPtr p;
(*p).bar;


No. When to compiler see's p->x. if p is a pointer type it does the
builtin member access thing, otherwise it calls p.operator->(). It
will continue doing this until operator->() returns a pointer when
it will do the builtin member accsess thing.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4

P: n/a
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
What you see above is called quoting, as long as it isn't over done
it helps people understand what you are talking about.

In this case: you seem to be responding to:
Is the expression

p->member

equivalent to this one?

(*p).member

I have a class that overloads both -> and *. Here is part of it.

class SharedPtr
{
Foo *p;
public:
Foo *operator->() { return p; }
Foo &operator*() { return *p; }
};

Is it a waste of effort implementing both operators or do I actually
need both? Say I write


You need to implement operator ->() if you want to use it.

SharedPtr p;
p->bar;

and I remove operator-> from SharedPtr, will that code still compile?
Does the compiler translate it into this?

SharedPtr p;
(*p).bar;


No. When to compiler see's p->x. if p is a pointer type it does the
builtin member access thing, otherwise it calls p.operator->(). It
will continue doing this until operator->() returns a pointer when
it will do the builtin member accsess thing.

Rob.
--
http://www.victim-prime.dsl.pipex.com/


Hey thanks that makes sense.
Jul 22 '05 #5

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f***************@News.CIS.DFN.DE...
You really should get yourself a C++ compiler.

Who said I didn't have a compiler? I have a compiler. I have VC++ 6.0.

Using Visual C++ 7.1:
error C2819: type 'SharedPtr' does not have an overloaded member 'operator ->'
Does the compiler translate it into this?

SharedPtr p;
(*p).bar;
No.

Just in case this is homework (I find it incredible that you don't have a
compiler) you'll have to look up the relevant paragraphs in the std

yourself.


Haha. I would not attempt C++ without a compiler. That would be plain
stupid!
Jul 22 '05 #6

P: n/a
> What you see above is called quoting, as long as it isn't over done
it helps people understand what you are talking about.
I know, sorry... I was in a hurry.
You need to implement operator ->() if you want to use it.


Well, in almost every class I've seen there isn't an overloaded ->()
operator... For example...

class ABC {
public:
ABC() { /* .... */ }
~ABC() { /* .... */ }
void write_something() {
std::cout << "something\n";
}
}

---

if i declared a ABC* abc, and then did abc->write_something(), it
wouldn't work??? (don't complain about possible mistakes in declaring
the class; haven't done that in a LOOOONG time, heh).

If not, I will throw any kinda book I have in front of me to the trash
can... lol
Jul 22 '05 #7

P: n/a
Chris Mantoulidis wrote:
What you see above is called quoting, as long as it isn't over done
it helps people understand what you are talking about.


I know, sorry... I was in a hurry.
You need to implement operator ->() if you want to use it.


Well, in almost every class I've seen there isn't an overloaded ->()
operator... For example...

class ABC {
public:
ABC() { /* .... */ }
~ABC() { /* .... */ }
void write_something() {
std::cout << "something\n";
}
}

---

if i declared a ABC* abc, and then did abc->write_something(),


But in this case 'abc' is defined to be a pointer. Therefore
abc->write_something() doesn't even try to use operator-> from
the class.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8

P: n/a

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:3f***********************@news.optusnet.com.a u...
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f***************@News.CIS.DFN.DE...
You really should get yourself a C++ compiler.


Who said I didn't have a compiler? I have a compiler. I have VC++ 6.0.


He was referring to the fact that you asked "and I remove operator-> from
SharedPtr, will that code still compile?", apparently without trying it.
Jul 22 '05 #9

P: n/a

and I remove operator-> from SharedPtr, will that code still compile? Does
the compiler translate it into this?

SharedPtr p;
(*p).bar;
No, the compiler doesn't convert equivelent operators. It won't do the
above
conversion, it won't combine == and ! to make !=, etc.... You have to
write
explicitly what you want.

Jul 22 '05 #10

P: n/a
>>You really should get yourself a C++ compiler.

Who said I didn't have a compiler? I have a compiler. I have VC++ 6.0.

... I find it incredible that you don't have a compiler ...


Haha. I would not attempt C++ without a compiler. That would be plain
stupid!


These are my personal favorite quotes of the day.

Jul 22 '05 #11

P: n/a
On Mon, 15 Dec 2003 10:16:31 -0800, Chris Mantoulidis wrote:
What you see above is called quoting, as long as it isn't over done
it helps people understand what you are talking about.


I know, sorry... I was in a hurry.
You need to implement operator ->() if you want to use it.


Well, in almost every class I've seen there isn't an overloaded ->()
operator... For example...


Because operator ->

as in:

FakePtr p;
p->doSomething();

Isn't the same as in

MyClass *p;
p->doSomething();
A pointer automatically has -> but you can use operator -> on a class to
allow you to use -> syntax on a class instance, this is probably used only
when you need to "pretend to be a pointer". The only place I have seen
this in code is in the implementation of auto_ptr (which I saw just a
minute ago because you made me look :) )
--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Jul 22 '05 #12

P: n/a
"Jeff Schwab" <je******@comcast.net> wrote in message
news:e6********************@comcast.com...
You really should get yourself a C++ compiler.

>
Who said I didn't have a compiler? I have a compiler. I have VC++ 6.0.

... I find it incredible that you don't have a compiler ...


Haha. I would not attempt C++ without a compiler. That would be plain
stupid!


These are my personal favorite quotes of the day.


I use VC++6 at work, and indeed it is a compiler. Whether it's a _C++_
compiler... well...

--
KCS
Jul 22 '05 #13

P: n/a

"Kevin Saff" <go********@kevin.saff.net> wrote in message
news:Hp********@news.boeing.com...


I use VC++6 at work, and indeed it is a compiler. Whether it's a _C++_
compiler... well...


That is kinda funny. You should see IBM C++ 3.6.5
Jul 22 '05 #14

P: n/a
On Mon, 15 Dec 2003 20:39:26 +1100, Jason Heyes wrote:
SharedPtr p;
p->bar;


besides meaning of both operators, it's worth noting what operator-> is
transitive. In other words - it's called again of its result up to the
point where it's no longer available.
B.
Jul 22 '05 #15

P: n/a
Bronek Kozicki wrote:
On Mon, 15 Dec 2003 20:39:26 +1100, Jason Heyes wrote:
SharedPtr p;
p->bar;

besides meaning of both operators, it's worth noting what operator-> is
transitive. In other words - it's called again of its result up to the
point where it's no longer available.
B.


Is the operator-> *always* transitive or just in the case that it has
not been overloaded?

If I had a class Foo with overloaded operator-> that returns a reference
to a Bar-object, what would exactly happen in the following cases:

Foo* a = ...;
Foo b;
// would this call Foo::some_method() or Bar::some_method()?
a->some_method();
// I suppose that would call Bar::humptydumpty()
// (using overloaded operator->)
b->humptydumpty();

Christof

Jul 22 '05 #16

P: n/a
> A pointer automatically has -> but you can use operator -> on a class to
allow you to use -> syntax on a class instance, this is probably used only
when you need to "pretend to be a pointer". The only place I have seen
this in code is in the implementation of auto_ptr (which I saw just a
minute ago because you made me look :) )


Iterators seem to have -> operator aswell if I recall correctly.
Jul 22 '05 #17

P: n/a

"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:br*************@news.t-online.com...

Is the operator-> *always* transitive or just in the case that it has
not been overloaded?

If I had a class Foo with overloaded operator-> that returns a reference
to a Bar-object, what would exactly happen in the following cases:

Foo* a = ...;
Foo b;
// would this call Foo::some_method() or Bar::some_method()?
a->some_method();


You can't overload operators on pointers. a-> ALWAYS uses the built in ->
operator regardless of what the type Foo does.
Jul 22 '05 #18

This discussion thread is closed

Replies have been disabled for this discussion.