Let ’ s look at how the code for the operator > () function works:
// Operator function for 'greater than' which
// compares volumes of CBox objects.
bool CBox::operator > (const CBox & aBox) const
{
return this- > Volume() > aBox.Volume();
}
You use a reference parameter to the function to avoid unnecessary copying when the function is
called. Because the function does not alter the object for which it is called, you can declare it as const . If you don ’ t do this, you cannot use the operator to compare const objects of type CBox at all. The return expression uses the member function Volume() to calculate the volume of the CBoxobject pointed to by this , and compares the result with the volume of the object aBoxusing the basic operator > . The basic > operator returns a value of type int (not a type bool), and thus, 1 is returned if the CBox object pointed to by the pointer this has a larger volume than the object aBox passed as a reference argument, and 0 otherwise. The value that results from the comparison will be automatically converted to the return type of the operator function, type bool.