Implicitly and Explicitly Using the this Pointer to Access an Object’s Data Members

Figure 9.23 demonstrates the implicit and explicit use of the this pointer to enable a member function of class Test to print the private data x of a Test object. In the next example and in Chapter 10, we show some substantial and subtle examples of using this.

 1   // Fig. 9.23: fig09_23.cpp 2   // Using the this pointer to refer to object members. 3   #include <iostream> 4   using namespace std; 5  6   class Test 7   { 8   public9      explicit Test( int = 0 ); // default constructor10      void print() const;11   private:12      int x;13   }; // end class Test14 15   // constructor16   Test::Test( int value )17      : x( value ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

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