When should you use 'friend' in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

The friend keyword in C++ allows a class or function to access the private and protected members of another class. While it provides flexibility, it should be used judiciously to maintain encapsulation and avoid tightly coupling classes. Here's when and why you might use friend:

When to Use friend in C++

1. Operator Overloading

You often use friend functions to overload certain operators that need to access private members of a class but aren’t naturally part of the class itself.

Example:

#include <iostream> using namespace std; class Point { private: int x, y; public: Point(int x, int y) : x(x), y(y) {} // Declare the operator<< as a friend friend ostream& operator<<(ostream& os, const Point& p); }; ostream& operator<<(ostream& os, const Point& p) { os << "(" << p.x << ", " << p.y << ")"; return os; } int main() { Point p(1, 2); cout << p << endl; // Output: (1, 2) }

The operator<< function needs access to private members x and y, so it is declared a friend of the Point class.

2. Allowing Specific Classes Access to Private Members

You can use friend to grant access to a specific class that requires access to the internal details of another class.

Example:

class Secret { private: int value; public: Secret(int v) : value(v) {} friend class Revealer; // Grant access to Revealer }; class Revealer { public: void reveal(const Secret& s) { cout << "Secret value is: " << s.value << endl; } }; int main() { Secret s(42); Revealer r; r.reveal(s); // Output: Secret value is: 42 }

This allows Revealer to access Secret's private members.

3. Tightly Coupled Classes

For classes that are closely related (e.g., they work together as part of the same module or logic), friend can simplify interaction by allowing direct access to each other's private data.

Example:

class Engine { private: int horsepower; public: Engine(int hp) : horsepower(hp) {} friend class Car; // Grant access to Car }; class Car { public: void showHorsepower(const Engine& e) { cout << "Engine horsepower: " << e.horsepower << endl; } }; int main() { Engine e(300); Car c; c.showHorsepower(e); // Output: Engine horsepower: 300 }

Here, Car needs to interact closely with Engine, so granting friend access avoids unnecessary getters/setters.

4. Access to Non-Member Helper Functions

For non-member utility functions that need to work with private data of a class, friend allows such functions to access private members without making them part of the class.

Example:

class Rectangle { private: int width, height; public: Rectangle(int w, int h) : width(w), height(h) {} friend int area(const Rectangle& r); }; int area(const Rectangle& r) { return r.width * r.height; } int main() { Rectangle rect(10, 20); cout << "Area: " << area(rect) << endl; // Output: Area: 200 }

The area function can directly access width and height because it is a friend.

Best Practices When Using friend

  1. Use Sparingly: Overusing friend can lead to tightly coupled code, which is harder to maintain and test.
  2. Preserve Encapsulation: Use friend only when absolutely necessary. If access can be achieved via public or protected methods, prefer those.
  3. Documentation: Clearly document why a friend is being used to ensure code maintainability and readability.
  4. Alternatives: Consider alternatives like getter/setter methods or protected inheritance when possible.

Summary

Use friend in C++ when:

  • Overloading operators like << or `>>.
  • Allowing tightly coupled classes or helper functions access to private data.
  • Simplifying access to private members for specific use cases.

For robust C++ design and coding patterns, consider exploring Grokking Advanced Coding Patterns for Interviews on DesignGurus.io, where such principles are explained in practical contexts.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to approach any system design interview problem?
Recalibrating study focus after each mock interview feedback
Does Apple negotiate salary?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.