+ 1
What is the diffrence between '.' operator and '->' operator in c++?
4 Réponses
+ 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