August 2012
Intermediate to advanced
976 pages
30h 17m
English
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
Read now
Unlock full access