Wrox Press C++ Tutorial
In our Box class, we wrote the function Volume() in terms of the class member names in the definition of the class. Of course, every object of type Box that we create contains these members, so there has to be a mechanism for the function to refer to the members of the particular object for which the function is called.
When any member function executes, it automatically contains a hidden pointer with the name this, which points to the object used with the function call. Therefore, when the member length is accessed in the function Volume() during execution, it's actually referring to this->length, which is the fully-specified reference to the object member that is being used. The compiler takes care of adding the necessary pointer name this to the member names in the function.
If you need to, you can use the pointer this explicitly within a member function. You might, for example, want to return a pointer to the current object.
We could add a public function to our class Box to compare the volume of two Box objects.
// Ex6_09.cpp
// Using the pointer this
#include <iostream>
using namespace std;
class Box // Class definition at global scope
{
public:
// Constructor definition
Box(double lv=1.0, double bv=1.0, double hv=1.0)
{
cout << endl << "Constructor called.";
length = lv; // Set values of
breadth = bv; // data members
height = hv;
}
// Function to calculate the volume of a box
double Volume()
{
return length * breadth * height;
}
// Function to compare two boxes which
// returns true if the first is greater
// than the second, and false otherwise
int compare(Box xBox)
{
return this->Volume() > xBox.Volume();
}
private:
double length; // Length of a box in inches
double breadth; // Breadth of a box in inches
double height; // Height of a box in inches
};
int main(void)
{
Box Match(2.2, 1.1, 0.5); // Declare Match box
Box Cigar(8.0, 5.0,1.0); // Declare Cigar box
if(Cigar.compare(Match))
cout << endl
<< "Match is smaller than Cigar";
else
cout << endl
<< "Match is equal to or larger than Cigar";
cout << endl;
return 0;
}
The member function compare() returns true if the prefixed Box object in the function call has a greater volume than the Box object specified as an argument, and false if it doesn't. In the return statements, the prefixed object is referred to through the pointer this, used with the indirect member access operator, ->, that we learned about earlier.
Remember that you use the direct member access operator . when dealing with objects and the indirect member access operator -> when dealing with pointers to objects. this is a pointer.
Using the pointer this in this example demonstrates that it exists and does work, but it's quite unnecessary to use it explicitly in this particular case. If you change the return statement in the compare() function to be,
return Volume() > xBox.Volume();
you'll find that it works just as well. Any references to unadorned member names are automatically assumed to be the members of the object pointed to by this.
The compare() function is used in main() to check the relationship between the volumes of the objects Match and Cigar. The output from the program is:

This confirms that the Cigar object is larger than the Match object.
It also wasn't essential to write the compare() function as a class member. We could just as well have written it as an ordinary function with the objects as arguments. Note that this isn't true of the function Volume(), since it needs to access the private data members of the class. Of course, if the function compare() was implemented as an ordinary function, it wouldn't have the pointer this, but it would still be very simple:
// Comparing two Box objects - ordinary function version
int compare(Box B1, Box B2)
{
return B1.Volume() > B2.Volume();
}
This has both objects as arguments and returns true if the volume of the first is greater than the last. You would use this function to perform the same function as in the last example with this statement:
if(compare(Cigar, Match))
cout << endl
<< "Match is smaller than Cigar";
else
cout << endl
<< "Match is equal to or larger than Cigar";
If anything, this looks slightly better and easier to read than the original version. However, there's a much better way to do this, which we shall see before the end of the chapter.