Friend Classes and Functions

Friend Functions vs. Public Members

When should I use friend functions instead of making members public?

Abstract art representing computer programming

Friend functions offer a way to grant specific access to private or protected members of a class without making those members public. Here’s when to use friend functions over public members:

Use Friend Functions When:

  1. Controlled Access: If only a specific function or class needs access to a member, using a friend function can prevent unnecessary exposure of internal data.
  2. Logical Grouping: If a function logically operates on the private data of a class but doesn’t naturally belong as a member function, making it a friend can be more appropriate.
  3. Operator Overloading: When overloading operators that should be non-member functions (like operator<<), friend functions can access the private data needed for the operation.

Example

Consider a BankAccount class where we want to allow a transferFunds function to access private balance data:

#include <iostream>

class BankAccount {
  friend void transferFunds(
    BankAccount &from,
    BankAccount &to,
    double amount
  );

public:
  BankAccount(double balance)
    : balance{balance} {}

  void displayBalance() const {
    std::cout << "Balance: " << balance << "\n";
  }

private:
  double balance;
};

void transferFunds(
  BankAccount &from,
  BankAccount &to,
  double amount
) {
  if (from.balance >= amount) {
    from.balance -= amount; 
    to.balance += amount; 
  }
}

int main() {
  BankAccount account1{100.0};
  BankAccount account2{50.0};

  transferFunds(account1, account2, 30.0);

  account1.displayBalance(); // Balance: 70
  account2.displayBalance(); // Balance: 80
}
Balance: 70
Balance: 80

In this example, transferFunds is a friend function that transfers money between two accounts.

Making balance public would expose it unnecessarily, while the friend function provides the needed access without compromising encapsulation.

When Not to Use Friend Functions

  • If the data needs to be accessible in many places, consider if it should be public or protected.
  • Overuse of friend declarations can make your code harder to understand and maintain.

Friend functions offer a balance between encapsulation and accessibility, but use them only when it makes logical and structural sense.

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