Friend Classes and Functions

Declaring Multiple Friends

What is the syntax for befriending multiple classes or functions at once?

Abstract art representing computer programming

In C++, you can declare multiple classes or functions as friends of a class. Each friend declaration must be on a separate line within the class definition.

This approach allows you to provide specific access to various classes and functions.

Syntax for Multiple Friend Functions

Here’s how to declare multiple friend functions within a class:

#include <iostream>

class MyClass {
  friend void functionOne(MyClass &obj);  
  friend void functionTwo(MyClass &obj);  

 private:
  int value{10};
};

void functionOne(MyClass &obj) {
  std::cout << "Value from functionOne: "
    << obj.value << "\n";
}

void functionTwo(MyClass &obj) {
  std::cout << "Value from functionTwo: "
    << obj.value << "\n";
}

int main() {
  MyClass obj;
  functionOne(obj);
  functionTwo(obj);
}
Value from functionOne: 10
Value from functionTwo: 10

Syntax for Multiple Friend Classes

Here’s how to declare multiple friend classes within a class:

#include <iostream>
class FriendClassOne;
class FriendClassTwo;

class MyClass {
  friend class FriendClassOne;  
  friend class FriendClassTwo;  

 private:
  int value{20};
};

class FriendClassOne {
 public:
  void showValue(MyClass &obj) {
    std::cout << "Value from FriendClassOne: "
      << obj.value << "\n";
  }
};

class FriendClassTwo {
 public:
  void showValue(MyClass &obj) {
    std::cout << "Value from FriendClassTwo: "
      << obj.value << "\n";
  }
};

int main() {
  MyClass obj;
  FriendClassOne f1;
  FriendClassTwo f2;

  f1.showValue(obj);
  f2.showValue(obj);
}
Value from FriendClassOne: 20
Value from FriendClassTwo: 20

Benefits of Multiple Friend Declarations

  • Specific Access: You can grant access to multiple classes or functions without exposing private members to the entire program.
  • Modularity: This maintains modularity by allowing only specific functions or classes to interact with the private data.

Practical Considerations

  • Readability: While declaring multiple friends can be useful, ensure it does not clutter the class definition, making it harder to read.
  • Design Choice: Overusing friend declarations might indicate a need to reconsider the design. Always assess if there are alternative design patterns that could achieve the same goal without exposing private members.

Conclusion

Befriending multiple classes or functions provides flexibility in controlling access to a class's private and protected members. This technique should be used judiciously to maintain clean and maintainable code.

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