Note the position of the prototype for the ordinary function version of operator > () . It needs to follow the class definition, because it refers to a CBox object in the parameter list. If you place it before the class definition, the example will not compile. There is a way to place it at the beginning of the program file following the #include statement: use an incomplete class declaration . This would precede the prototype and would look like this:
class CBox; // Incomplete class declaration
bool operator > (const double & value, const CBox & aBox); // Function prototype
The incomplete class declaration identifies CBox to the compiler as a class and is sufficient to allow the compiler to process the prototype for the function properly, since the compiler now knows that CBox is a user - defined type to be specified later. This mechanism is also essential in circumstances such as those where you have two classes, each of which has a pointer to an object of the other class as a member. They will each require the other to be declared first. It is only possible to resolve such an impasse through the use of an incomplete class declaration.
The output from the example is:
Constructor called.
Constructor called.
mediumBox is bigger than smallBox
mediumBox capacity is more than 50
smallBox capacity is less than 10
Destructor called.
Destructor called.
After the constructor messages due to the declarations of the objects smallBox and mediumBox , you have the output lines from the three if statements, each of which is working as you would expect. The first of these calls the operator function that is a class member and works with two CBox objects. The second calls the member function that has a parameter of type double . The expression in the third if statement calls the operator function that you have implemented as an ordinary function. As it happens, you could have made both the operator functions that are class members ordinary functions, because they only need access to the member function Volume() , which is public