Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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

 
 
Howard
Guest
Posts: n/a
 
      06-23-2004

"Kapt. Boogschutter" <(E-Mail Removed)> wrote in message
news:cbc91c$jel$(E-Mail Removed)1.nb.home.nl...
> "raj" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed) 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


>



 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      06-24-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.
>
> 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
 
Reply With Quote
 
 
 
 
Steven T. Hatton
Guest
Posts: n/a
 
      06-24-2004
Mike Wahler wrote:

> "raj" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
a
Guest
Posts: n/a
 
      06-24-2004

"Ali Cehreli" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) ...
> 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';
> }


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.


 
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