473,487 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why have both "." and "->" ?

raj
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.

Raj
Jul 22 '05 #1
23 1608
"raj" <ra******@hotmail.com> schreef in bericht
news:d7**************************@posting.google.c om...
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.
Raj


There is a difference I believe that xxxx->yyyy is used if xxxx is a pointer
to an object/class and xxxx.yyyyy if xxxx is the object/classs
Jul 22 '05 #2
> class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.

IMHO it's simply convenience.

Bye, Marco

Jul 22 '05 #3
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both
operators.


The second form involves more typing and requires more effort to read.

--
Russell Hanneken
eu*******@cbobk.pbz
Use ROT13 to decode my email address.
Jul 22 '05 #4
In message <WO*****************@newsread1.news.pas.earthlink. net>,
Russell Hanneken <me@privacy.net> writes
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both
operators.


The second form involves more typing and requires more effort to read.

And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.

--
Richard Herring
Jul 22 '05 #5
raj wrote:
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.

Raj


The reason is obvious: for compatibility with C.

--
Pull out a splinter to reply.
Jul 22 '05 #6
"raj" <ra******@hotmail.com> wrote in message
news:d7**************************@posting.google.c om...
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both

operators.

The -> operator is not technically necessary, it's just
a 'shorthand' notation for (*).

Use whichever you like, but keep in mind that ->
is typically considered more 'idomatic' (i.e.
most coders will recognize it, and often makes
reading code faster.)

-Mike
Jul 22 '05 #7
On 23 Jun 2004 08:48:27 -0700, ra******@hotmail.com (raj) wrote:
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.


You can separately overload operator-> and unary operator* for user
defined types. That's the only difference really, but aa->f() is much
nicer to read.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #8

"raj" <ra******@hotmail.com> wrote in message
news:d7**************************@posting.google.c om...
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both

operators.

The second is pretty awkward, but anyway, what would you do with this?
A aa;
Jul 22 '05 #9

"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...

The second is pretty awkward, but anyway, what would you do with this?
A aa;


Never mind, didn't read your question quite right. You are not implying
that we don't need ".".
Jul 22 '05 #10
raj wrote:
You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.

The short answer to your question is, "because C did it that way." C++
was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask Dennis
Ritchie. There probably is a good reason.

Brian Rodenborn
Jul 22 '05 #11
raj wrote:
...
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.
...


Well, any boolean function can be implemented by using logical operation
'xor' (or 'nor', or 'nand') and nothing else. Yet instead of single
'xor' we have 'and' (&&), 'or' (||) and 'not' (!) in the language. Why?
The make code easier to read. The same applies to '->' operator. In many
situations it produces more compact and easily readable code.

And it also gives us an additional overloadable operator.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #12
Richard Herring posted:
In message <WO*****************@newsread1.news.pas.earthlink. net>,
Russell Hanneken <me@privacy.net> writes
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need
both operators.


The second form involves more typing and requires more effort to read.

And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.

Sounds like bullshit.

-JKop
Jul 22 '05 #13
On Wed, 23 Jun 2004 11:27:08 -0700, JKop wrote:
Richard Herring posted:
In message <WO*****************@newsread1.news.pas.earthlink. net>,
Russell Hanneken <me@privacy.net> writes
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need
both operators.

The second form involves more typing and requires more effort to read.

And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.

Sounds like bullshit.

-JKop


But it is not:

struct Type0
{
int foo() const
{
return 42;
}
};

struct Type1
{
int foo() const
{
return 7;
}
};

struct Proxy
{
Type0 type0_;
Type1 type1_;

Type0 & operator* ()
{
return type0_;
}

Type1 * operator-> ()
{
return &type1_;
}
};

#include <iostream>

int main()
{
Proxy p;
std::cout << (*p).foo() << '\n';
std::cout << p->foo() << '\n';
}
Jul 22 '05 #14
JKop wrote:
Richard Herring posted:

In message <WO*****************@newsread1.news.pas.earthlink. net>,
Russell Hanneken <me@privacy.net> writes
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need
both operators.

The second form involves more typing and requires more effort to read.


And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.


Sounds like bullshit.

-JKop


Look up "smart pointers" on google. Then tell us it's bullshit.
Jul 22 '05 #15
Ali Cehreli posted:
struct Type0
{
int foo() const
{
return 42;
}
};

struct Type1
{
int foo() const
{
return 7;
}
};

struct Proxy
{
Type0 type0_;
Type1 type1_;

Type0 & operator* ()
{
return type0_;
}

Type1 * operator-> ()
{
return &type1_;
}
};

#include <iostream>

int main()
{
Proxy p;
std::cout << (*p).foo() << '\n';
std::cout << p->foo() << '\n';
}

I stand corrected.

-JKop
Jul 22 '05 #16
Default User wrote:
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.


The short answer to your question is, "because C did it that way." C++
was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask Dennis
Ritchie. There probably is a good reason.


C++ allows overloading of -> but not .

V
Jul 22 '05 #17
Victor Bazarov posted:
Default User wrote:
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need
both operators.


The short answer to your question is, "because C did it that way." C++
was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask
Dennis Ritchie. There probably is a good reason.


C++ allows overloading of -> but not .


He refering to how one can overload * .

If one overloads both * and -> for a class, and makes them different, then
the following is no longer equal:

SomeClass jk;

jk->Chocolate();

(*jk).Chocolate();
-JKop
Jul 22 '05 #18
Victor Bazarov wrote in
news:el******************@dfw-read.news.verio.net in comp.lang.c++:
Default User wrote:
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need
both operators.


The short answer to your question is, "because C did it that way."
C++ was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask
Dennis Ritchie. There probably is a good reason.


C++ allows overloading of -> but not .


I'm guessing that Default Users's point was, that given:

struct X
{
int x;
};

struct X xx, *xp = &x;

'C' could have done this:

Have xp.x be an int * pointing to xx.x and then *xp.x would have
derefrenced it (i.e. today's xp->x), no need for (*xp).x or xp->x.

I've encontered one C compiler that actually did this, no idea
wether it was a feature or a bug :).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #19
Victor Bazarov wrote:

Default User wrote:
raj wrote:

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.


The short answer to your question is, "because C did it that way." C++
was not about to redefine the usage. So why did C?

For the case above, the reply is obvious, syntactic sugar. The better
question is, why didn't C overload the . operator to work on either
struct/union objects or on pointers? For that, you'd have to ask Dennis
Ritchie. There probably is a good reason.


C++ allows overloading of -> but not .

But if there was no -> operator, then there would probably be a way to
overload the . operator.

I think that's a result of having two operators, not a cause. As in the
developer(s) didn't think to themselves, "It'd be greate to get rid of
that unnecessary -> but let's keep it so we don't have to allow
overloading the . operator."

The reason there's two is that C did it that way. As a consequence, C++
could take advantage by allowing overloads for one but not the other.

Brian Rodenborn
Jul 22 '05 #20

"Kapt. Boogschutter" <so*****@nobody.com> wrote in message
news:cb**********@news5.tilbu1.nb.home.nl...
"raj" <ra******@hotmail.com> schreef in bericht
news:d7**************************@posting.google.c om...
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both operators.

Raj


There is a difference I believe that xxxx->yyyy is used if xxxx is a

pointer to an object/class and xxxx.yyyyy if xxxx is the object/classs

That's true, but look at the question again. The poster explicitly used
(*aa).f(), and that's using a de-referenced pointer, which is the same thing
as your xxxx.yyyy example.

His question was why the "aa->f()" form is needed if you can accomplish it
using "(*aa).f()".

I would guess it's a convenience. It's sure easier to type, in my opinion!

-Howard


Jul 22 '05 #21
raj wrote:
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both
operators.

Raj


After reviewing some of the replies to your question, you may be asking "Why
are people in this news group so obnoxious, condescending, and rude?". I
certainly am asking, but I can't answer that question.

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #22
Mike Wahler wrote:
"raj" <ra******@hotmail.com> wrote in message
news:d7**************************@posting.google.c om...
I used to remember why c++ needed both ?
Could somebody help me here ?

For example

class A{
f();
};

A* aa;

You could do either "aa->f()" or "(*aa).f()". So why does C++ need both

operators.

The -> operator is not technically necessary, it's just
a 'shorthand' notation for (*).

Use whichever you like, but keep in mind that ->
is typically considered more 'idomatic' (i.e.
most coders will recognize it, and often makes
reading code faster.)

-Mike


Now that's an answer worth providing.
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #23
a

"Ali Cehreli" <ac******@yahoo.com> wrote in message
news:pa*********************************@yahoo.com ...
On Wed, 23 Jun 2004 11:27:08 -0700, JKop wrote:
Richard Herring posted:
In message <WO*****************@newsread1.news.pas.earthlink. net>,
Russell Hanneken <me@privacy.net> writes
raj wrote:
>
> You could do either "aa->f()" or "(*aa).f()". So why does C++ need
> both operators.

The second form involves more typing and requires more effort to read.

And they might have different effects if aa is of user-defined type.
Usually, operator->() returns (something that behaves like) a pointer;
operator*() returns a reference. Either or both might be some kind of
proxy object, not the object that aa ultimately "points" at. There's no
guarantee that they indirect to the same thing, or even that they are
both defined.

Sounds like bullshit.

-JKop


But it is not:

struct Type0
{
int foo() const
{
return 42;
}
};

struct Type1
{
int foo() const
{
return 7;
}
};

struct Proxy
{
Type0 type0_;
Type1 type1_;

Type0 & operator* ()
{
return type0_;
}

Type1 * operator-> ()
{
return &type1_;
}
};

#include <iostream>

int main()
{
Proxy p;
std::cout << (*p).foo() << '\n';
std::cout << p->foo() << '\n';
}


Thats not the same behavior the original poster was asking about. Of course
if you overload the operators to do different things they will behave
differently.
Jul 22 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2554
by: Ollej Reemt | last post by:
Hello, I would like to know if there is a difference in c++ between the following two method-declarations: void Method(); and void Method(void);
8
10622
by: Lian | last post by:
Hi all, It is a newbie's question about html tag "img". The attributes "title" and "alt" for "img" seems having the same function. So what is the main difference between them? Can i use them at...
5
3434
by: Mateusz Loskot | last post by:
Hi, I'd like to ask how XML parsers should handle attributes which consists of &quot; entity as value. I know XML allows to use both: single and double quotes as attribute value terminator. That's...
2
1958
by: John Baker | last post by:
Hi: I have two systems, one a W98 and the other XP Home. I have Access 2000 installed on both, and have run into a difference in the way the two behave. I have a table on which I wish to reset...
81
7219
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
8
3354
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
5
4371
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
4
1561
by: JMUApache | last post by:
Hi, A have a problem with Inhreit Control. I try to Change its Size in Construct Function, And it works. Code Here: -------------------------------------------------- public class...
5
3166
by: comp.lang.php | last post by:
$orderBy = 's.app_date desc, s.last_name asc, s.first_name asc, s.mi asc'; if ($_REQUEST) { $ascArray = array('asc' => 'desc', 'desc' => 'asc'); // ARRAY OF ALL ORDERING POSSIBILITIES $junk =...
94
30183
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
6967
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
7181
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
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
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,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.