Inheriting Friend Functions

Can friend functions be inherited by derived classes?

Friend functions are not inherited by derived classes. This is because the friend relationship is specific to the class where the friend function is declared. Here's how it works:

Understanding Friend Functions

When you declare a friend function in a class, you're granting it access to the private and protected members of that specific class. This does not extend to derived classes. Each class must explicitly declare its own friends.

Example

Consider a base class Base and a derived class Derived. If Base declares a friend function, this friend function does not automatically become a friend of Derived.

#include <iostream>

class Base {
  friend void showData(Base &b, bool derived);  

 private:
  int baseData{10};
};

class Derived : public Base {
 private:
  int derivedData{20};
  // Note: showData is not a friend here.
};

void showData(Base &b, bool showDerived = false) {
  std::cout << "\nBase data: " << b.baseData;  

  if (showDerived) {
    std::cout << "\nDerived data: "
      << static_cast<Derived &>(b).derivedData;  
  }
}

int main() {
  Base b;
  Derived d;
  showData(b, false);
  showData(d, true); 
}
error C2248: 'Derived::derivedData': cannot access private member declared in class 'Derived'

In this example, showData() is a friend of Base and can access its private members. However, it is not a friend of Derived, so it cannot access the private derivedData member.

Making a Friend Function for Derived Classes

If you want a friend function to access members of both base and derived classes, you must declare it as a friend in each class:

#include <iostream>

class Base {
  friend void showData(Base &b, bool derived);

 private:
  int baseData{10};
};

class Derived : public Base {
  friend void showData(Base &b, bool derived);  

 private:
  int derivedData{20};
};

void showData(Base &b, bool showDerived = false) {
  std::cout << "\nBase data: " << b.baseData;

  if (showDerived) {
    std::cout << "\nDerived data: "
      << static_cast<Derived &>(b).derivedData;
  }
}

int main() {
  Base b;
  Derived d;
  showData(b, false);
  showData(d, true);
}
Base data: 10
Base data: 10
Derived data: 20

By declaring showData() as a friend in both Base and Derived, it can now access private members of both classes. This approach keeps the encapsulation intact while granting necessary access where needed.

Friend Classes and Functions

An introduction to the friend keyword, which allows classes to give other objects and functions enhanced access to its members

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Friend Functions and Encapsulation
How do friend functions and classes affect encapsulation?
Friend Functions vs. Public Members
When should I use friend functions instead of making members public?
Self-Friend Class
Can a class befriend itself, and if so, what are the use cases?
Friend Function for Multiple Classes
Can a friend function access members of multiple classes?
Declaring Multiple Friends
What is the syntax for befriending multiple classes or functions at once?
Real-World Examples of Friend Functions
What are some real-world examples of using friend functions effectively?
Inline Friend Functions
Can a friend function be declared inline, and what are the implications?
Friend Functions and Virtual Inheritance
How do friend functions and classes work with virtual inheritance?
Overloaded Friend Functions
What happens if a friend function is overloaded?
Friend Functions in Namespaces
Can we have a friend function in a namespace?
Alternatives to Friend Functions
Are there any alternatives to using friend functions for accessing private data?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant