Friend Classes and Functions

Self-Friend Class

Can a class befriend itself, and if so, what are the use cases?

Abstract art representing computer programming

In C++, a class can befriend itself, though it is not a common practice and typically not necessary.

Declaring a class as its own friend doesn't provide any additional access or capabilities because the class already has access to its own private and protected members.


Here’s how you would declare a class as its own friend:

class SelfFriend {
  friend class SelfFriend;

  int value{42};

  int getValue() const { return value; }

In this example, SelfFriend is declared as a friend of itself. However, this declaration does not change the access level of value since SelfFriend already has access to all its members.

Use Cases

  1. Template Specializations: One possible use case is in template specializations, where a class template might need to access private members of a specific instantiation of itself.
  2. Complex Inheritance Structures: In some rare and complex inheritance structures, a class might declare itself as a friend to clarify access control or future-proof against changes in access requirements.

Here’s an example with template specializations:

#include <iostream>

template <typename T>
class Container {
  friend class Container<int>; 

  T data;

  Container(T val) : data{val} {}
  T getData() const { return data; }

template <>
class Container<int> {
  friend class Container<double>; 

  int data;

  Container(int val) : data{val} {}
  int getData() const { return data; }

int main() {
  Container<int> intContainer{100};
  std::cout << intContainer.getData() << "\n";

  Container<double> doubleContainer{3.14};
  std::cout << doubleContainer.getData() << "\n";

In this example, the Container<int> specialization befriends Container<double>, allowing Container<double> to access private members of Container<int>.

This can be useful in specialized template classes where different instantiations need access to each other's internals.


While befriending itself is rarely necessary, it can be useful in certain advanced scenarios like template specializations or complex inheritance hierarchies.

However, for most practical purposes, a class does not need to befriend itself since it inherently has access to its own members.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved