+ 1
Why is -> sign used
2 odpowiedzi
+ 15
a->b is just a nicer syntax than the equivalent (*a).b
+ 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;
}