Examples
Team LiB
Previous Section Next Section

Examples

Example 1: Operators. The streaming operator<< and operator>> for objects of some class type X are perhaps the most compelling examples of functions that are clearly part of the interface of the class X, but which are always nonmember functions (this is of necessity, because the left-hand argument is a stream, not an X). The same argument applies to other nonmember operators on X objects. Make sure that your operators appear in the same namespace as the class on which they operate. When you have the option, prefer making operators and all other functions nonmember nonfriends (see Item 44).

Example 2: Other functions. If the author of X supplies named helper functions that take X objects, they should be supplied in the same namespace, otherwise calling code that uses X objects will not be able to use the named functions without explicit qualification or a using statement.

    Team LiB
    Previous Section Next Section