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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access