-
Book Overview & Buying
-
Table Of Contents
C++ Memory Management
By :
C++ offers an access qualifier that’s not commonly found in other languages and is often misunderstood: the friend qualifier. A class can specify another class or a function as one of its friends, giving said friend qualifier full access to all of that class’s members, including those qualified as protected or private.
Some consider friend to break encapsulation, and indeed it can do this if used recklessly, but the intent here is to provide privileged access to specific entities rather than exposing them as public or protected members that were not designed to that end, leading to an even wider encapsulation breakage.
Consider, for example, the following classes, where thing is something that is meant to be built from the contents of a file named name by a thing_factory that’s able to validate the file’s content before constructing the thing:
class thing {
thing(string_view); // note: private
// ... various...