a->b()
, or a.b()
?
a
is a pointer, use: a->b()
a
is an object, use: a.b()
a
is a reference, then it’s an alias for something--use the
rules for whatever it refers to.
->
for objects—we’ll learn about that later.
vector<bool> v(42, true); // v is a vector of bool cout << v.size() << '\n';
42
Here, we use a dot, because v
is an object, not a pointer.
vector<bool> v(42, true); // v is a vector of bool vector<bool> *p = &v; // p is a pointer to vector of bool cout << (*p).size() << ' ' << p->size() << '\n';
42 42
Two ways (one ugly, one not) of doing the same thing.
vector<bool> v(42, true); // v is a vector of bool vector<bool> &r = v; // r is a reference to a vector of bool cout << r.size() << '\n';
42
r
is a reference to an object.
r
.