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

Discussion in 'C++' started by raj, Jun 23, 2004.

  1. raj

    raj Guest

    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
    raj, Jun 23, 2004
    #1
    1. Advertising

  2. "raj" <> schreef in bericht
    news:...
    > 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
    Kapt. Boogschutter, Jun 23, 2004
    #2
    1. Advertising

  3. raj

    Marco Wahl Guest

    > 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
    Marco Wahl, Jun 23, 2004
    #3
  4. 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

    Use ROT13 to decode my email address.
    Russell Hanneken, Jun 23, 2004
    #4
  5. In message <WOhCc.15115$>,
    Russell Hanneken <> 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
    Richard Herring, Jun 23, 2004
    #5
  6. raj

    Peter Ammon Guest

    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.
    Peter Ammon, Jun 23, 2004
    #6
  7. raj

    Default User Guest

    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
    Default User, Jun 23, 2004
    #7
  8. raj

    Mike Wahler Guest

    "raj" <> wrote in message
    news:...
    > 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
    Mike Wahler, Jun 23, 2004
    #8
  9. raj

    jeffc Guest

    "raj" <> wrote in message
    news:...
    > 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;
    jeffc, Jun 23, 2004
    #9
  10. raj

    jeffc Guest

    "jeffc" <> wrote in message
    news:...
    >
    > 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 ".".
    jeffc, Jun 23, 2004
    #10
  11. raj

    tom_usenet Guest

    On 23 Jun 2004 08:48:27 -0700, (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
    tom_usenet, Jun 23, 2004
    #11
  12. 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
    Andrey Tarasevich, Jun 23, 2004
    #12
  13. raj

    JKop Guest

    Richard Herring posted:

    > In message <WOhCc.15115$>,
    > Russell Hanneken <> 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
    JKop, Jun 23, 2004
    #13
  14. raj

    red floyd Guest

    JKop wrote:
    > Richard Herring posted:
    >
    >
    >>In message <WOhCc.15115$>,
    >>Russell Hanneken <> 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.
    red floyd, Jun 23, 2004
    #14
  15. raj

    Ali Cehreli Guest

    On Wed, 23 Jun 2004 11:27:08 -0700, JKop wrote:

    > Richard Herring posted:
    >
    >> In message <WOhCc.15115$>,
    >> Russell Hanneken <> 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';
    }
    Ali Cehreli, Jun 23, 2004
    #15
  16. raj

    JKop Guest

    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
    JKop, Jun 23, 2004
    #16
  17. 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
    Victor Bazarov, Jun 23, 2004
    #17
  18. raj

    JKop Guest

    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
    JKop, Jun 23, 2004
    #18
  19. Victor Bazarov wrote in
    news:ellCc.1510$ 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/
    Rob Williscroft, Jun 23, 2004
    #19
  20. raj

    Default User Guest

    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
    Default User, Jun 23, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,816
    Smokey Grindel
    Dec 2, 2006
  2. GS
    Replies:
    19
    Views:
    1,614
    Gordon Burditt
    Dec 15, 2004
  3. ABCL
    Replies:
    0
    Views:
    545
  4. Robert Zurer

    Why is it that a TableCell cannot have both Text and a Child Control?

    Robert Zurer, Aug 21, 2003, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    158
    Teemu Keiski
    Aug 22, 2003
  5. Dennis Yurichev
    Replies:
    2
    Views:
    234
    Dennis Yurichev
    Dec 6, 2012
Loading...

Share This Page