Friend Classes and Functions

Real-World Examples of Friend Functions

What are some real-world examples of using friend functions effectively?

Abstract art representing computer programming

Friend functions can be very useful in real-world programming when used appropriately. Here are a few scenarios where friend functions are effective:

1. Operator Overloading

A common use case for friend functions is in operator overloading. For example, overloading the << operator to print class objects.

#include <iostream>

class Point {
  friend std::ostream& operator<<(
    std::ostream &os, const Point &p); 

public:
  Point(int x, int y) : x{x}, y{y} {}

private:
  int x, y;
};

std::ostream& operator<<(
  std::ostream &os, const Point &p) {
  os << "Point(" << p.x << ", " << p.y << ")";
  return os;
}

int main() {
  Point p(3, 4);
  std::cout << p;
}
Point(3, 4)

Here, the << operator is overloaded to print a Point object, accessing its private members directly.

2. Logging and Debugging

Friend functions can be used for logging and debugging by accessing private members and providing detailed logs.

#include <iostream>

class Server {
  friend void logServerState(const Server &server);  

 public:
  Server(int activeConnections, int maxConnections)
    : activeConnections{activeConnections},
      maxConnections{maxConnections} {}

 private:
  int activeConnections;
  int maxConnections;
};

void logServerState(const Server &server) {
  std::cout << "Active Connections: "
    << server.activeConnections << "\n";
  std::cout << "Max Connections: "
    << server.maxConnections << "\n";
}

int main() {
  Server server(120, 200);
  logServerState(server);
}
Active Connections: 120
Max Connections: 200

In this example, logServerState() is a friend function that logs the private state of the Server class.

3. Helper Functions

Friend functions can serve as helper functions that perform specific tasks which require access to the internal state of a class.

#include <iostream>

class Rectangle {
  friend int calculateArea(const Rectangle &rect);  

 public:
  Rectangle(int width, int height)
    : width{width}, height{height} {}

 private:
  int width, height;
};

int calculateArea(const Rectangle &rect) {
  return rect.width * rect.height;
}

int main() {
  Rectangle rect(10, 20);
  std::cout << "Area: " << calculateArea(rect);
}
Area: 200

Here, calculateArea() is a friend function that calculates the area of a Rectangle, accessing its private dimensions.

Conclusion

Friend functions can be effectively used in operator overloading, logging, debugging, and helper functions.

They provide a way to access private data without breaking encapsulation, but 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