Declaring Multiple Friends

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

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.

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?
Inheriting Friend Functions
Can friend functions be inherited by derived classes?
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?
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