Dot . Vs Arrow Access ->..? | Sololearn: Learn to code for FREE!

+9

Dot . Vs Arrow Access ->..?

Both can be used to access member of a User Defined DataType but why should we even bother to use -> instead of .(dot operator) ...I mean we have to create a pointer of that data type...store the address of the object then we can use the -> Why use -> instead of dot operator ...

1/27/2018 9:37:49 AM

Cyberspace Cyborg

2 Answers

New Answer

+2

Often you don't want to pass a copy of a structure around, but rather a reference to an existing structure. That's only possible using a pointer. If you'd create a "normal" struct from a pointer again just to be able to use the dot operator (Foo bar = *baz) you will have a copy of the structure again and you won't actually modify the fields of the original structure. So the arrow operator is incredibly useful for saving memory by avoiding copies and modifying existing data in other functions.

+3

@Cyperspace, You use "dot operator" when your objects are allocated on the stack. You can use "dot operator" if your objects are allocated on heap(dynamic allocation) but remember dereferencing consumes cpu cycles Use arrow notation (->) when you have pointers or pointer like variables like iterators in C/C++. Also remember you can overload arrow operator(->) for your class. But dot operator cannot be overloaded Kindly add C/C++ tag to your question.