기준을 묻는다
public API만으로 구현할 수 있다면 friend 없이도 객체의 계약이 유지된다.
friend 선언은 특정 함수나 타입에 내부 접근권을 준다. 편하다는 이유보다 객체의 의미를 밖에서 자연스럽게 표현해야 할 때만 써야 한다.
public API만으로 구현할 수 있다면 friend 없이도 객체의 계약이 유지된다.
클래스 전체 friend보다 특정 free function 하나에만 접근권을 준다.
private 멤버 이름이 바뀔 때 friend 코드도 함께 깨지는 비용을 설계에 반영한다.
class Point {
public:
Point(int x, int y) : x_(x), y_(y) {}
friend std::ostream& operator<<(std::ostream& os, const Point& p);
private:
int x_, y_;
};