Accessing Data Members Using Pointers

You accessed data members and functions by using the dot (.) operator for Cat objects created locally. To access the Cat object on the heap, you must dereference the pointer and call the dot operator on the object pointed to by the pointer. Therefore, to access the GetAge member function, you would write

 (*pRags).GetAge();

Parentheses are used to assure that pRags is dereferenced before GetAge() is accessed.

Because this is cumbersome, C++ provides a shorthand operator for indirect access: the points-to operator (->), which is created by typing the dash (-) immediately followed by the greater-than symbol (>). C++ treats this as a single symbol. Listing 10.2 demonstrates accessing member variables and functions ...

Get Sams Teach Yourself C++ in 24 Hours, Third 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.