1

What is difference in expressions foo->bar() and (*foo).bar()?

Those choices has equal results. So the code:

string *str = new string("Hello");

cout<<str->c_str()<<endl;
cout<<(*str).c_str()<<endl;

prints:

Hello
Hello

Maybe foo->bar() and (*foo).bar() difference in assembly code and memory usage?

iEPCBM
  • 211
  • 3
  • 6
  • 6
    The difference is two extra key strokes, and I'm not being snarky. For raw pointers that is literally the difference. class-level operator overloads can cowboy up different behavior, but that's not what you're showing. – WhozCraig Jan 15 '22 at 12:54
  • I count three extra keystrokes. `-`+`SHIFT`+`>`, that's three, versus `SHIFT`+`(`+`*` then `SHIFT`+`)`+`.`, that's six. – Sam Varshavchik Jan 15 '22 at 12:59
  • https://en.cppreference.com/w/cpp/language/operator_member_access – 463035818_is_not_an_ai Jan 15 '22 at 13:05
  • Just because you can do something, doesn’t mean you should. The correct way to access members through pointers in C++ is `->`. Full stop. – Taekahn Jan 15 '22 at 16:43

0 Answers0