Operator Placement

좌측 피연산자가 배치 방식을 정한다

멤버 연산자는 왼쪽 객체가 this가 되고, 전역 연산자는 두 피연산자를 모두 매개변수로 받아 스트림 연산에도 맞출 수 있다.

멤버 함수

p1 + p2는 p1이 this

Point Point::operator+(const Point& other) const;
p1.operator+(p2);

좌측 피연산자가 Point 객체라면 멤버 함수로 자연스럽게 표현할 수 있다.

전역 함수

std::cout << p는 ostream이 왼쪽

std::ostream& operator<<(std::ostream& os, const Point& p);

왼쪽이 std::ostream이므로 Point의 멤버 함수가 아니라 비멤버 함수로 둔다.

private 접근

전역 operator가 x, y 같은 private 멤버를 직접 읽어야 한다면 클래스 안에서 friend로 선언해 접근 권한을 명시한다.