"friend"가 캡슐화를 망치나요?
그렇지 않습니다. friend는 회원권 처럼 접근권한을 얻는 명시적인 메커니즘입니다. 소스를 수정없이 스스로 클래스의 접근 권한을 얻을 수 없습니다. 예를 들자면 :
class X {
int i;
public:
void m(); // grant X::m() access
friend void f(X&); // grant f(X&) access
// ...
};
void X::m() { i++; /* X::m() can access X::i */ }
void f(X& x) { x.i++; /* f(X&) can access X::i */ }
c++의 보호 모델의 설명은 D&E sec 2.10, TC++PL sec 11.5, 15.3, C.11을 참고하세요
원문
Does "friend" violate encapsulation?
No. It does not. "Friend" is an explicit mechanism for granting access, just like membership. You cannot (in a standard conforming program) grant yourself access to a class without modifying its source. For example:
class X {
int i;
public:
void m(); // grant X::m() access
friend void f(X&); // grant f(X&) access
// ...
};
void X::m() { i++; /* X::m() can access X::i */ }
void f(X& x) { x.i++; /* f(X&) can access X::i */ }
For a description on the C++ protection model, see D&E sec 2.10 and TC++PL sec 11.5, 15.3, and C.11.
'번역 > Bjarne Stroustrup's C++ Style and Technique FAQ' 카테고리의 다른 글
Why do my compiles take so long? (4) | 2019.01.15 |
---|---|
Why doesn't my constructor work right? (4) | 2019.01.15 |
Why isn't the destructor called at the end of scope? (2) | 2018.08.13 |
How do i define an in class constant? (0) | 2018.08.12 |
Why is the size of an empty class not zero? (0) | 2018.05.10 |
댓글