By the way, having an operator double() member function defined would create confusion at this point because that would create another option for interpretation. Instead of converting kennyD to double and performing Stonewt addition, the compiler could convert jennySt to double and perform double addition. Having too many conversion functions creates ambiguities.
Finally,
total = pennyD + jennySt;
becomes
total = operator+(pennyD, jennySt); // friend function
Here, both arguments are type double, which invokes the Stonewt(double) constructor to convert them to Stonewt objects.
However, the member function version wouldn’t be able to add jennySt to pennyD. Converting the addition syntax to a function call would look like this:
total = pennyD.operator+(jennySt); // not meaningful
But this is meaningless because only a class object can invoke a member function. C++ does not attempt to convert pennyD to a Stonewt object. Conversion takes place for member function arguments, not for member function invokers.
The lesson here is that defining addition as a friend makes it easier for a program to accommodate automatic type conversions. The reason is that both operands become function arguments, so function prototyping comes into play for both operands.
Choices in Implementing Addition
Given that you want to add double quantities to Stonewt quantities, you have a couple choices. The first, as you just saw, is to define the following as a friend function and have the Stonewt(double) constructor handle conversions of type double arguments to type Stonewt arguments:
operator+(const Stonewt &, const Stonewt &)
The second choice is to further overload the addition operator with functions that explicitly use one type double argument:
Stonewt operator+(double x); // member function
friend Stonewt operator+(double x, Stonewt & s);
That way, the following statement exactly matches the operator+(double x) member function:
total = jennySt + kennyD; // Stonewt + double
And the following statement exactly matches the operator+(double x, Stonewt & s) friend function:
total = pennyD + jennySt; // double + Stonewt
Earlier, we did something similar for Vector multiplication.
Each choice has advantages. The first choice (relying on implicit conversions) results in a shorter program because you define fewer functions. That also implies less work for you and fewer chances to mess up. The disadvantage is the added overhead in time and memory needed to invoke the conversion constructor whenever a conversion is needed. The second choice (additional functions explicitly matching the types), however, is the mirror image. It makes for a longer program and more work on your part, but it runs a bit faster.
If your program makes intensive use of adding double values to Stonewt objects, it may pay to overload addition to handle such cases directly. If the program uses such addition only occasionally, it’s simpler to rely on automatic conversions, or if you want to be more careful, on explicit conversions.
Summary
This chapter covers many important aspects of defining and using classes. Some of the material in this chapter may seem vague to you until your own experiences enrich your understanding.
Normally, the only way you can access private class members is by using a class method. C++ alleviates that restriction with friend functions. To make a function a friend function, you declare the function in the class declaration and preface the declaration with the keyword friend.
C++ extends overloading to operators by letting you define special operator functions that describe how particular operators relate to a particular class. An operator function can be a class member function or a friend function. (A few operators can only be class member functions.) C++ lets you invoke an operator function either by calling the function or by using the overloaded operator with its usual syntax. An operator function for the operator
operator
result = up.operator+(right);
result = up + right;
For the second version, the fact that the operands up and right are type Vector tells C++ to use the Vector definition of addition.