Friend Classes and Functions

Friend Function for Multiple Classes

Can a friend function access members of multiple classes?

Abstract art representing computer programming

Yes, a friend function can access members of multiple classes if each class declares that function as a friend. This allows the function to have privileged access to the private and protected members of each class.

Example

Consider two classes, ClassA and ClassB, both granting friendship to the same function sharedFunction:

#include <iostream>

// Forward declarations
class ClassA;
class ClassB;

class ClassA {
  friend void sharedFunction(ClassA &a, ClassB &b);  

 private:
  int dataA{5};
};

class ClassB {
  friend void sharedFunction(ClassA &a, ClassB &b);  

 private:
  int dataB{10};
};

void sharedFunction(ClassA &a, ClassB &b) {
  std::cout << "ClassA data: " << a.dataA << "\n";
  std::cout << "ClassB data: " << b.dataB << "\n";
}

int main() {
  ClassA a;
  ClassB b;
  sharedFunction(a, b);
}
ClassA data: 5
ClassB data: 10

In this example, sharedFunction is a friend to both ClassA and ClassB. This enables it to access the private data members dataA and dataB from instances of these classes.

Benefits

  • Centralized Functionality: A friend function can centralize operations that involve multiple classes, making the code easier to manage.
  • Encapsulation Maintenance: It allows access to private data without making it public, thus preserving encapsulation.

Use Cases

  1. Inter-Class Operations: Useful for functions that need to operate on data from multiple classes, such as combining, comparing, or transferring data between objects.
  2. Operator Overloading: Common in operator overloading where the operation requires access to private data of different types.

Here’s an example with operator overloading:

#include <iostream>

// Forward declarations
class ClassX;
class ClassY;

class ClassX {
  friend bool operator==(
    const ClassX &x, const ClassY &y);  

 private:
  int value{3};
};

class ClassY {
  friend bool operator==(
    const ClassX &x, const ClassY &y);  

 private:
  int value{3};
};

bool operator==(
  const ClassX &x, const ClassY &y) {
  return x.value == y.value;  
}

int main() {
  ClassX x;
  ClassY y;
  if (x == y) {
    std::cout << "Equal\n";
  } else {
    std::cout << "Not equal\n";
  }
}
Equal

In this example, the operator== function is a friend to both ClassX and ClassY, allowing it to access their private value members and compare them.

Conclusion

Friend functions that access members of multiple classes can be powerful tools for maintaining encapsulation while enabling specific inter-class operations.

This approach should be used thoughtfully to avoid overly complex dependencies between classes.

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:

  • 125 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