Difficulty: Intermediate
What are friend functions and friend classes, and do they break encapsulation? What does the inline keyword really do?
These two features look unrelated, but they often appear as a pair on interview lists of 'C++ keywords you should know'. Each has a myth attached, so let's tackle both.
A friend declaration grants a non-member function or another class access to the private and protected members of your class. It is like giving a trusted neighbour a spare key to your house: the neighbour is not part of the family (not a member), but you have deliberately decided to let them in. The declaration lives inside the class, so the class itself controls who gets access, which is why friendship does not really destroy encapsulation; it extends the class's public interface to a chosen set of collaborators. The classic legitimate uses are stream operators (operator<< must be a non-member), symmetric binary operators, and tightly coupled helper classes like an iterator and its container.
Some properties to remember. Friendship is not mutual: if A declares B a friend, A cannot automatically access B's private members. It is not transitive: a friend of my friend is not my friend. It is not inherited: a friend of Base does not become a friend of Derived. A friend function is not a member and has no this pointer. Overusing friends creates tight coupling, so use them sparingly.
Now inline. Most people say 'it makes the compiler paste the function body at the call site to avoid call overhead'. That was the original motive, but in modern C++ the keyword is only a hint that compilers freely ignore. Optimisers inline aggressively on their own using their own heuristics, and can inline functions not marked inline while refusing marked ones (recursive functions, big bodies, or calls through function pointers and virtual calls resolved at runtime).
The one thing inline really does, and it is guaranteed by the language, concerns the One Definition Rule. A function or variable marked inline may be defined in multiple translation units as long as the definitions are identical, and the linker merges them. That is why functions defined in headers must be inline: without it, including the header in two .cpp files gives 'multiple definition' linker errors. Functions defined inside a class body are implicitly inline. C++17 extended this to variables: 'inline constexpr int kMax = 10;' in a header gives a single shared entity, and 'static inline' data members can be initialised inside the class.
Trade-offs: aggressive inlining removes call overhead and enables further optimisation, but it grows the binary and can hurt the instruction cache. Small getters and tiny helpers are ideal. constexpr functions are implicitly inline.
So the honest interview answer is: friend is a controlled exception to access rules; inline is primarily about linkage (ODR) and only secondarily a hint about optimisation.
#include <iostream>
class Box {
double w_, h_;
public:
Box(double w, double h) : w_(w), h_(h) {}
friend double area(const Box& b); // non-member with private access
friend class BoxInspector; // friend class
};
double area(const Box& b) { return b.w_ * b.h_; }
class BoxInspector {
public:
static double width(const Box& b) { return b.w_; }
};
// Would normally live in a header: inline makes multiple definitions legal.
inline int square(int x) { return x * x; }
inline constexpr int kMax = 10; // C++17 inline variable
int main() {
Box b(3, 4);
std::cout << area(b) << " " << BoxInspector::width(b) << "\n";
std::cout << square(kMax) << "\n";
}
area and BoxInspector can read Box's private members because Box declared them as friends. The inline keyword on square and kMax lets a header define them without linker errors.
friend, inline, encapsulation, ODR, constexpr