What is the diffrence between '.' operator and '->' operator in c++? | SoloLearn: Learn to code for FREE!

+1

What is the diffrence between '.' operator and '->' operator in c++?

7/2/2020 2:46:48 PM

Anurag Pandey

4 Answers

New Answer

+6

Shouldn't be asking this after completing both C and C++ Anyways '.' is a member access operator normally used to access members of objects. '->' arrow operator is used for same purpose, but you have pointer in hand instead of an object struct C { int b; }; C c; c.b = 10; C* cptr = &c; cptr->b = 20 = (*cptr).b;

+4

. if you are using an object directly or a reference to it. -> if you are using a pointer to the object.

+2

In simple human readable words, speech.sayHi(). speech->sayHi() Here "." accesses method of 'speech'. "->" accesses method of dereferenced "speech" or *speech. Therefore, '.' = speech.sayHi() '->' = (*speech).sayHi() You do not necessarily have to use the arrow oprator instead you can deference the class and use the normal dot operator. Also if you are wondering when will I have to dereference a class. This a example: Speech* sp1 = new Speech(); Have a good day, Ćheyat