Friend class
A friend class in C++ can access the private and protected members of the class in which it is declared as a friend.[src] A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.[src] The friend class mechanism allows to extend the storage and access to the parts, while retaining proper encapsulation as seen by the users of the data structure.[src] Similar to a friend class, a friend function is a function that is given access to the private and protected members of the class in which it is declared as a friend.[src] Since C++26, C++ supports "variadic friends" (i.e.[src] friend classes that come from variadic templates).[src]
A friend class in C++ can access the private and protected members of the class in which it is declared as a friend.[src] A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.[src] The friend class mechanism allows to extend the storage and access to the parts, while retaining proper encapsulation as seen by the users of the data structure.[src] Similar to a friend class, a friend function is a function that is given access to the private and protected members of the class in which it is declared as a friend.[src] Since C++26, C++ supports "variadic friends" (i.e.[src] friend classes that come from variadic templates).[src] ==Example== The following example demonstrates the use of a friend-class for a graph data structure, where the graph is represented by the main class Graph, and the graph's vertices are represented by the class Vertex.[src] import std; template using SharedPtr = std::shared_ptr ; using String = std::string; template using UniquePtr = std::unique_ptr ; template using HashSet = std::unordered_set ; // default-specialize the remaining template-parameters class Vertex { private: // Vertex gives access-rights to Graph.[src] friend class Graph; HashSet > edges; String name; using EdgesIterator = decltype(edges.cbegin()); public: explicit Vertex(const String& name): name{std::move(name)} {} EdgesIterator begin() const { return edges.cbegin(); } EdgesIterator end() const { return edges.cend(); } String getName() const { return name; } }; class Graph { private: HashSet > vertices; using VerticesIterator = decltype(vertices.cbegin()); public: ~Graph() { while (!vertices.empty()) { VerticesIterator vertex = vertices.begin(); removeVertex(*vertex); } } void addVertex(const String& name) { UniquePtr vertex = std::make_unique (name); VerticesIterator iter = vertices.insert(vertex.get()); } void removeVertex(SharedPtr vertex) { vertices.erase(vertex); } void addEdge(SharedPtr from, SharedPtr to) { // Graph can access Vertex's private fields because Vertex declared Graph as // a friend.[src] from->edges.insert(to); } VerticesIterator begin() const { return vertices.cbegin(); } VerticesIterator end() const { return vertices.cend(); } }; Using variadic friends: import std; template class ClassWithFriends { private: int secretValue; friend Friends...; public: explicit ClassWithFriends(int secret): secretValue{secret} {} }; class A { public: void readSecret(const class ClassWithFriends & instance) const { std::println("Inspecting secret value from A: {}", instance.secretValue); } }; class B { public: void readSecret(const class ClassWithFriends & instance) const { std::println("Inspecting secret value from B: {}", instance.secretValue); } }; int main(int argc, char* argv[]) { ClassWithFriends secretHolder(135); A a; B b; a.readSecret(secretHolder); b.readSecret(secretHolder); } ==Encapsulation== A proper use of friend classes increases encapsulation, because it allows to extend the private access of a data-structure to its parts which the data-structure owns without allowing private access to any other external class.[src] This way the data-structure stays protected against accidental attempts at breaking the invariants of the data-structure from outside.[src] It is important to notice that a class cannot give itself access to another class's private part; that would break encapsulation.[src] Rather, a class gives access to its own private parts to another class by declaring that class as a friend.[src] In the graph example, Graph cannot declare itself a friend of Vertex.[src] Rather, Vertex declares Graph a friend, and so provides Graph an access to its private fields.[src] The fact that a class chooses its own friends means that friendship is not symmetric in general.[src] In the graph example, Vertex cannot access private fields of Graph, although Graph can access private fields of Vertex.[src] ==Alternatives== A similar, but not equivalent, language feature is given by C#'s internal access modifier keyword, which allows classes inside the same assembly to access the private parts of other classes.[src] This corresponds to marking each class a friend of another in the same assembly; friend classes are more fine-grained.[src] Programming languages which lack support for friend classes, or a similar language feature, will have to implement workarounds to achieve a safe part-based interface to a data-structure.[src] Examples of such workarounds are: Make the parts' fields public.[src] This solution decreases encapsulation by making it possible to violate invariants of the data-structure from outside.[src] Move all mutable structural data away from the part to the data-structure, and introduce indirection back from each part to its data-structure.[src] This solution changes the organization of the data structure, and increases memory consumption in cases where there would otherwise be no need for this information.[src] ==Properties== Friendships are not symmetric – if class A is a friend of class B , class B is not automatically a friend of class A .[src] Friendships are not transitive – if class A is a friend of class B , and class B is a friend of class C , class A is not automatically a friend of class C .[src] Friendships are not inherited – if class Base is a friend of class X , subclass Derived is not automatically a friend of class X ; and if class X is a friend of class Base , class X is not automatically a friend of subclass Derived .[src] However, if class Y is a friend of subclass Derived , class Y will also have access to protected portions of class Base , just as subclass Derived does.[src] ==References== ==External links== http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr043.htm http://www.cplusplus.com/doc/tutorial/inheritance/ de:Friend-Klasse[src]
35 sentences · 35 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Friend class." encyclopegia.org, 2025, https://encyclopegia.org/wiki/friend-class. Accessed 02 December 2026.
Chicago
"Friend class." encyclopegia.org. Accessed December 2, 2026. https://encyclopegia.org/wiki/friend-class.
APA
encyclopegia.org. (2025). Friend class. Retrieved December 2, 2026, from https://encyclopegia.org/wiki/friend-class
Accessed December 2, 2026 · CC BY-SA 4.0 · citation page · JSON