Is -> syntactic sugar?

J

Jason Heyes

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.
 
R

Ron Natalie

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.
 
C

Chris Mantoulidis

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
 
A

Alf P. Steinbach

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.
 
R

Rob Williscroft

Chris Mantoulidis wrote in
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.
 
J

Jason Heyes

Rob Williscroft said:
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:


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


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.

Hey thanks that makes sense.
 
J

Jason Heyes

Alf P. Steinbach said:
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 ->'



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!
 
C

Chris Mantoulidis

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
 
K

Karl Heinz Buchegger

Chris said:
I know, sorry... I was in a hurry.


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";
}
}

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.
 
J

jeffc

Jason Heyes said:
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.
 
J

Jeff Schwab

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.

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

These are my personal favorite quotes of the day.
 
N

Nils Petter Vaskinn

I know, sorry... I was in a hurry.


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 :) )
 
K

Kevin Saff

Jeff Schwab said:
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...
 
B

Bronek Kozicki

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.
 
C

Christof Krueger

Bronek said:
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
 
W

wogston

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.
 
R

Ron Natalie

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.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top