E D R , A S I H C RSS

BackLinks search for "CharProxy"

BackLinks of CharProxy


Search BackLinks only
Display context of search results
Case-sensitive searching
  • MoreEffectiveC++/Techniques2of3
          class CharProxy { // 문자의 프록시
          CharProxy(String& str, int index); // 생성
          CharProxy& operator=(const CharProxy& rhs); // lvalue
          CharProxy& operator=(char c); // 의 쓰임에 반응
          const CharProxy operator[](int index) const; // const String에 반응
          CharProxy operator[](int index); // non-const String을 위해서
          friend class CharProxy;
         Item 29에서의 String클래스의 최종버전과 이 소스가 다른점은 오직 추가된 CharProxy클래스에 operator[] 함수가 구현되었다는 점이다. 하지만, 클라이언트 입장에서는 보통 쓰는것처럼 String클래스를 사용할때 이러한 사실을 무시하고, operator[]함수가 글자를 반환하는 것으로 취급한다.
         s1[5]의 표현은 CharProxy 객체를 반환한다. s1[5]가 output(<<) 연산자에 대하여 객체에 대하여 정의된것은 없다. 그래서 당신의 컴파일러는 operator<<에 적용할수 있는 암시적(implicit) 형변환을 찾는다. 컴파일러는 그래서 프록시 클래스 내부에 선언되어 있는 char()를 찾을수 있다. 컴파일러는 이(char) 형변환을 수행하기를 요청하고, 결과적으로 CharProxy는 문자로 변환되어서 출련되어 진다. 다시 말하지만, 이것은 CharProxy-to-char 로의 형변환이 CharProxy내부에 암시적(implicit) 형변환이 선언되어 있기 때문이다.
         s2[5]의 표현은 CharProxy객체를 반환한다. 그리고 할당(assignment)연산자의 목표가 된다.어떤 할당(assignment) 연산자가 불려지는 걸까? 할당의 목표는 CharProxy이다. 그래서 할당연산자는 CharProxy 클래스 안에서 불려진다. 이것은 중요한 것이다. 왜냐하면 CharProxy의 할당(assignment) 연산자를 사용하는것으로 우리는 Stirng에서 lvalue로서 이번 연산이 수행된다는 것을 알수있다. 그래서 우리는 문자열 클래스가 이번에는 lvalue에 알맞는 동작을 해야 한다는 결론을 얻는다.
         이것은 두개의 CharProxy를 위해서 할당 연산자가 동작하고, 하나는 char으로 암시적 변환, 또 하나는 CharProxy객체의 할당 연산자를 사용하는 것
         const String::CharProxy String::operator[](int index) const
          return CharProxy(const_cast<String&>(*this), index);
         String::CharProxy String::operator[](int index)
          return CharProxy(*this, index);
         각 함수는 문자 요구시에 CharProxy 객체를 만들어서 반환한다. 문자열은 스스로는 아무 일을 하지 못한다. 우리는 그러한 동작을 읽기와 쓰기의 접근을 알수있을때 까지 지연시키도록 만들어야 한다.
         const 버전의 operator[] 는 const proxy 객체를 반환해야 하는 것을 보자. CharProxy::operator=은 const 멤버 함수가 이니기 때문에 할당(assignment)의 목표가 되지 않는다. 그러므로, proxy는 const 버전의 operator[]나, lvalue로서의 문자열의 사용을 고려하지 않아도 된다. 간단히, const 버전의 operator[]에서도 정확히 돌아간다는 이야기이다.
         이번에는 CharProxy를 만들때 const버전의 operator[]에서 const_cast(Item 2참고)를 사용해서 *this를 넘기는걸 주목하자.저것은 CharProxy생성자에 조건에 부합하기 위한 수행으로, non-const String만 인자로 받기위해서 형변환을 수행한다. 형변환은 보통은 귀찮다. 그렇지만 이러한 경우에 CharProxy 객체는 그것 자체가 const이기 때문에 String가 포함하고 있는 proxy가 참조하는 String은 수정되어지는 걱정이 없을 것이다.
         String::CharProxy::CharProxy(String& str, int index)
         String::CharProxy::operator char() const
Found 1 matching page out of 7540 total pages

You can also click here to search title.

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
Processing time 0.0059 sec