Friend functions and classes

The friend declaration lets a class specify that a function or class has access to its private or protected members. Avoid friend declarations for loosely coupled classes because they control access at a very coarse granularity and don't specify what's acceptable and what's not (unless you document it). Taligent's preferred alternative is to define internal use only public member functions and denote with a comment what they are for (see "Class definition conventions" on page 63).

Friends make perfect sense for some situations, such as tightly coupled classes like TSetOf and TSetIteratorOver from the Collection classes. These classes are implemented in tandem and must know about one another's implementation, so it's appropriate to make them friend classes. Also, overloaded operators or functions must sometimes be global for symmetry, and therefore often must be declared as friend. A Taligent engineer must confer with an architect before making friend declarations.

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker