Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Discussion

Both public member functions and nonmember functions form part of the public interface of a class. The Interface Principle states: For a class X, all functions (including nonmember functions) that both “mention” X and are “supplied with” X in the same namespace are logically part of X, because they form part of X’s interface. (See Item 44 and [Sutter00].)

The C++ language is explicitly designed to enforce the Interface Principle. The reason why argument-dependent lookup (ADL, also known as Koenig lookup) was added to the language was to ensure that code that uses an object x of type X can use its nonmember function interface (e.g., cout << x, which invokes the nonmember operator<< for X) as easily as it can use member functions (e.g., x.f(), which requires no special lookup because f is clearly to be looked up in the scope of X). ADL ensures that nonmember functions that take X objects and that are supplied with X’s definition can participate as first-class members of X’s interface, just like X’s direct member functions naturally do.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial