473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is -> syntactic sugar?

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
17 2706
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
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
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
"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
"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
> 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
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

"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

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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christopher T King | last post by:
Okay, so this is really two requests in one, and they're both kinda outlandish, but I'm gonna post them nonetheless: I've always thought xrange() to be ugly; it looks to be a lot of typing just...
5
by: F Jamitzky | last post by:
It is rather easy to define functions in python that mimic the special ruby syntactic sugar like: 5.times { print "Hello World!" } or .each { |food| eat food } In python these fragments...
13
by: Neil Zanella | last post by:
Hello, It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have...
4
by: Bas | last post by:
Hi group, just out of curiosity, is there a list of all the syntactic sugar that is used in python? If there isn't such a list, could it be put on a wiki somewhere? The bit of sugar that I do...
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
13
by: Sam Kong | last post by:
Hi, While discussing C#'s using statement, a guy and I had an argument. In C# spec (15.13), there's an explanation like the following. using (R r1 = new R()) { r1.F(); } is precisely...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.