Knowing Your Objects: The this Pointer

There's still more to be done with the Stock class. So far each class member function has dealt with but a single object, which has been the object that invokes it. Sometimes, however, a method might need to deal with two objects, and doing so may involve a curious C++ pointer called this. Let's see how this need can unfold.

Although the Stock class declaration displays data, it's deficient in analytic power. For example, by looking at the show() output you can tell which of your holdings has the greatest value, but the program can't tell because it can't access total_val directly. The most direct way of letting a program know about stored data is to provide methods to return values. Typically, you use inline ...

Get C++ Primer Plus, Fourth 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.