0

Possible Duplicate:
What is the difference between the dot (.) operator and -> in C++?

What's the difference between using dot notation and the pointer way?

Instantiating an object with or without a pointer.

Instantiate w/o a pointer = then use dot notation

Instantiate w/ a pointer = then use ->

What are the differences between both? When and why should one be used over the other?

Community
  • 1
  • 1
RoR
  • 13,894
  • 21
  • 65
  • 92
  • Are you asking when you should use dynamic versus automatic allocation? Or when you use the dot operator versus the arrow operator? – John Dibling Dec 23 '10 at 20:31
  • 1
    Duplicate of: http://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot-operator-and-in-c – Charles Salvia Dec 23 '10 at 20:32
  • See http://stackoverflow.com/questions/599308/proper-stack-and-heap-usage-in-c http://stackoverflow.com/questions/102009/when-is-it-best-to-use-a-stack-instead-of-a-heap-and-vice-versa http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c http://stackoverflow.com/questions/679571/when-to-use-new-and-when-not-to-in-c – Josh Lee Dec 23 '10 at 20:32

1 Answers1

5

If I understand your question: in C++, a->b is just shorthand for (*a).b -- they're exactly the same (Edit: unless you've overloaded them to behave differently!), it's just that the first is easier to type. :)

If you're referring to using string a; versus string* a = new string(), that's a different topic -- look up stack-based and heap-based allocation.

user541686
  • 189,354
  • 112
  • 476
  • 821
  • Not necesarrily. Both `operator*` and `operator->` are overloadable. – John Dibling Dec 23 '10 at 20:33
  • I'm not sure what you mean -- could you give an example of overloading `operator*` in this context? (Is that multiplication or dereferencing?) – user541686 Dec 23 '10 at 20:33
  • Thank you. Will mark as answer when it allows. I am not trying to do any operator overloading. The answer given is what I needed. – RoR Dec 23 '10 at 20:34
  • The dereference and arrow operators can both be overloaded to do somethign entirely different. – John Dibling Dec 23 '10 at 20:34
  • @Lambert: Think iterator. C++ iterators have to have `operator*` work like `*` does for a pointer, whatever it takes. – David Thornley Dec 23 '10 at 20:35
  • Actually...in C++ this is only *usually* true, depending on what 'a' is. – Edward Strange Dec 23 '10 at 20:35
  • Ohh... okay, now I see what you mean, thanks. :) I've personally never programmed or used an overloaded version of the dereferencing operator (indices and `->` have always been enough), so that's why I was surprised. – user541686 Dec 23 '10 at 20:36