can anyone tell me the merits & demerits of friend functions?
p.s. help me
kushal

Dani AI

Generated

, the key point missing above is that friend does not break encapsulation when used deliberately. A class explicitly grants access; that grant becomes part of the class’s public contract just like its member functions. In other words, friendship is a tool for carefully exposing internals to specific helpers, not a back door. See Bjarne Stroustrup’s explanation and note how he frames friends as an explicit access mechanism, not a violation. (stroustrup.com)

When friends shine:

  • Symmetric non-member operators (==, <, +, <<) that conceptually belong outside the class but need access to internals. This also enables conversions on both operands, which members cannot. (isocpp.org)
  • Tightly coupled collaborators with different lifetimes/instance counts (e.g., iterator and container) where exposing getters would leak representation. (isocpp.org)
  • The Virtual Friend Function Idiom: a free function interface that still dispatches via a protected virtual. (isocpp.org)

Cautions:

  • Overuse couples code and can hide poor design, as hinted. Prefer narrow friend declarations (friend a function, not a whole class) and keep invariants enforced by the class. Friendship is not inherited, transitive, or reciprocal, so grant it intentionally. (isocpp.org)

Small, practical pattern you can drop in today (echoing the links and shared, but with a concrete example):

#include <ostream>

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

operator<< is a non-member (good design), yet it needs access to Point’s representation, so we friend it. For language details and edge cases (templates, non-transitive rules), see the cppreference entry on friend declarations. (en.cppreference.com)

Recommended Answers

All 9 Replies

the only time i can think of using friend functions is for operator overloading.. the rest of the time they should be avoided because they violate OOP data encapsulation.

>the rest of the time they should be avoided because they violate OOP data encapsulation.
If this is what you think, then you're not in a position to help. Those who still need to learn shouldn't be teaching.

or this

thanxs sunnypalsingh, it was a great help...

what r merits & demerits of friend function

please tell me merits and demerits of friend function

Uh. Follow the links posted already in the thread? (Try not to be an unthinking idiot who is not inclined to read the replies already posted here.)

Uh. Follow the links posted already in the thread? (Try not to be an unthinking idiot who is not inclined to read the replies already posted here.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.