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{

    A* aa;

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

    raj, Jun 23, 2004
    1. Advertisements

  2. 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
    1. Advertisements

  3. raj

    Marco Wahl Guest

    class A{
    IMHO it's simply convenience.

    Bye, Marco
    Marco Wahl, Jun 23, 2004
  4. The second form involves more typing and requires more effort to read.
    Russell Hanneken, Jun 23, 2004
  5. 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, Jun 23, 2004
  6. raj

    Peter Ammon Guest

    The reason is obvious: for compatibility with C.
    Peter Ammon, Jun 23, 2004
  7. raj

    Default User Guest

    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
  8. raj

    Mike Wahler Guest


    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 Wahler, Jun 23, 2004
  9. raj

    jeffc Guest


    The second is pretty awkward, but anyway, what would you do with this?
    A aa;
    jeffc, Jun 23, 2004
  10. raj

    jeffc Guest

    Never mind, didn't read your question quite right. You are not implying
    that we don't need ".".
    jeffc, Jun 23, 2004
  11. raj

    tom_usenet Guest

    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_usenet, Jun 23, 2004
  12. 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.
    Andrey Tarasevich, Jun 23, 2004
  13. raj

    JKop Guest

    Richard Herring posted:

    Sounds like bullshit.

    JKop, Jun 23, 2004
  14. raj

    red floyd Guest

    Look up "smart pointers" on google. Then tell us it's bullshit.
    red floyd, Jun 23, 2004
  15. raj

    Ali Cehreli Guest

    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
  16. raj

    JKop Guest

    Ali Cehreli posted:

    I stand corrected.

    JKop, Jun 23, 2004
  17. C++ allows overloading of -> but not .

    Victor Bazarov, Jun 23, 2004
  18. raj

    JKop Guest

    Victor Bazarov posted:
    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;



    JKop, Jun 23, 2004
  19. Victor Bazarov wrote in
    in comp.lang.c++:
    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 Williscroft, Jun 23, 2004
  20. raj

    Default User Guest

    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.