Why is -> sign used | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is -> sign used

1st Jan 2018, 11:51 AM
TECHNICAL PROGRAMMER
TECHNICAL PROGRAMMER - avatar
2 Answers
+ 15
a->b is just a nicer syntax than the equivalent (*a).b
1st Jan 2018, 11:56 AM
Eric Blinkidu
Eric Blinkidu - avatar
+ 5
Use dot to access instance members, use arrow (->) to access instance pointer members. #include <iostream> using namespace std; class Car { public: string Brand; string Color; Car(string brand, string color) :Brand(brand), Color(color) {} }; int main() { // Object member access use dot (.) Car car1("Ferrari", "Red"); cout << "Car #1 is a " << car1.Color << " " << car1.Brand << endl; // Object pointer member access use (->) Car* car2 = new Car("Lamborghini", "Black"); cout << "Car #2 is a " << car2->Color << " " << car2->Brand << endl; delete car2; return 0; }
1st Jan 2018, 12:42 PM
Ipang