friend cho phép một hàm hoặc class truy cập trực tiếp các thành viên private/protected của class khác — phá encapsulation có kiểm soát.
class Box {
double width_;
public:
Box(double w) : width_(w) {}
friend double getWidth(const Box& b); // hàm bạn
};
double getWidth(const Box& b) {
return b.width_; // truy cập private trực tiếp
}Khi dùng hợp lý:
- Operator overloading cần truy cập private của cả 2 bên (ví dụ operator<<).
- Unit test class cần inspect nội bộ.
- 2 class cộng tác chặt chẽ mà getter/setter sẽ quá lộn xộn.
Lưu ý: friend không kế thừa — class con không tự động là friend. Dùng tiết kiệm; quá nhiều friend thường là dấu hiệu thiết kế cần refactor.
friend lets a function or class directly access the private/protected members of another class — controlled encapsulation breakage.
class Box {
double width_;
public:
Box(double w) : width_(w) {}
friend double getWidth(const Box& b); // friend function
};
double getWidth(const Box& b) {
return b.width_; // direct private access
}Legitimate uses:
- Operator overloading needing private access on both sides (e.g. operator<<).
- Unit test classes that need to inspect internals.
- Two tightly cooperating classes where getters/setters would be overly noisy.
Note: friend is not inherited — subclasses are not automatically friends. Use sparingly; too many friend declarations usually signal a design issue.