As you've seen, C++ controls access to the private portions of a class object. Usually, public class methods serve as the only access, but somethimes this restriction is too rigid to fit particular programming problems. In such cases, C++ provides another form of access: the friend.
Friends come in three varieties:
- Friend functions
- Friend classes
- Friend member functions
By making a function friend to a class, you allow the function the same access privileges that a member function of the class has. We'll look into friend functions now, leaving the other two varieties to another blog.
A = 2.75 * B; // cannot correspond to a member function
Thus the compiler could match this expression to the following nonmember function call:
A = operator*(double m, const Time & t);
With the nonmember overloaded operator function, the left operand of an operator expression corresponds to the first argument of the operator function, and the right operand corresponds to the second argument. Meanwhile, the original member function handles operands in the opposite order--that is , a Time value multiplied by a double value.
Using a nonmember function solves the problem of getting the operands in the desired order (first double and then time), but it raises a new problem: Nonmember functions can't directly access private data in a class. Well, at least ordinary nonmember functions lack access. But there is a special category of nonmember functions, called friends that can access private members of a class.
Creating Friends
The first step toward creating a friend function is to place a prototype in the class declaration and prefix the declaration with the keyword friend:
friend Time operator*(double m, const Time & t); // goes in class declaration
This prototype has two implications:
- Although the operator*() function is declared in the class declaration, it is not a member function. SO it isn't invoked by using the membership operator
- Although the operator*() function is not a member function, it has the same access rights as a member function.
The second step is to write the function definition. Because it is not a member function, you don't use the Time:: qualifier. Also, you don't use the friend keyword in the definition. The definition should look like this:
Time operator*(double mult, const Time & t) // friend not used in definition
{
Time result;
long totalminutes = t.hours * mult * 60 + t.minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
With this declaration and definition, the statement
A = 2.75 * B;
translates to
A = operator*(2.75, B);
and invokes the nonmember friend function you just defined.
In short, a friend function to a class is a nonmember function that has the same access rights as a member function.
Remember:
A nonmember version of an overloaded operator function require as many formal parameters as the operator has operands. A member version of the same operator requires one fewer parameter because one operand is passed implicitly ad the invoking object, the this pointer ;)
Abonneren op:
Reacties posten (Atom)
Geen opmerkingen:
Een reactie posten