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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.