Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why have both "." and "->" ?

Reply
Thread Tools

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

 
 
tom_usenet
Guest
Posts: n/a
 
      06-23-2004
On 23 Jun 2004 08:48:27 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      06-23-2004
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

 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      06-23-2004
Richard Herring posted:

> In message <WOhCc.15115$(E-Mail Removed). net>,
> Russell Hanneken <(E-Mail Removed)> 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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-23-2004
JKop wrote:
> Richard Herring posted:
>
>
>>In message <WOhCc.15115$(E-Mail Removed). net>,
>>Russell Hanneken <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ali Cehreli
Guest
Posts: n/a
 
      06-23-2004
On Wed, 23 Jun 2004 11:27:08 -0700, JKop wrote:

> Richard Herring posted:
>
>> In message <WOhCc.15115$(E-Mail Removed). net>,
>> Russell Hanneken <(E-Mail Removed)> 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';
}
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      06-23-2004
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-23-2004
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      06-23-2004
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
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      06-23-2004
Victor Bazarov wrote in
news:ellCc.1510$(E-Mail Removed) 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/
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-23-2004
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
 
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
Why does C have both . and -> for addressing struct members? Dennis Yurichev C++ 2 12-06-2012 02:49 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why in stdint.h have both least and fast integer types? GS C Programming 19 12-15-2004 10:15 PM
Why is it that a TableCell cannot have both Text and a Child Control? Robert Zurer ASP .Net Building Controls 4 08-22-2003 07:23 PM



Advertisments