What is the diffrence between '.' operator and '->' operator in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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

2nd Jul 2020, 2:46 PM
Anurag Pandey
Anurag Pandey - avatar
2 Answers
+ 4
. if you are using an object directly or a reference to it. -> if you are using a pointer to the object.
2nd Jul 2020, 9:45 PM
Sonic
Sonic - avatar
+ 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
2nd Jul 2020, 2:53 PM
Ćheyat
Ćheyat - avatar