4.6. The Member Access Operators
The dot (§ 1.5.2, p. 23) and arrow (§ 3.4.1, p. 110) operators provide for member access. The dot operator fetches a member from an object of class type; arrow is defined so that ptr->
mem is a synonym for (*
ptr)
.mem:
string s1 = "a string", *p = &s1;auto n = s1.size(); // run the size member of the string s1n = (*p).size(); // run size on the object to which p pointsn = p->size(); // equivalent to (*p).size()
Because dereference has a lower precedence than dot, we must parenthesize the dereference subexpression. If we omit the parentheses, this code means something quite different:
// run the size member of p, then dereference the result!*p.size(); // error: p is a pointer and has no member named
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.