Herb Sutter의 More Exceptional C++의 item3을 보면...

predicate가 복사되어도 상태를 일관되게 관리하기 위해서

자체적으로 제작한 스마트 포인터(CountedPtr)을 이용한 예를 볼 수 있다.

아무리 봐도 이상한 점은 아래 코드에서 빨간색으로 표시한 대입연산자에서

왜 Decrement를 호출해줘야 하는가 이다... auto_ptr 처럼 소유권이 이전되는게

아닌데... 아무리 봐도 이해가 되질 않는다...-OTL

( Herb Sutter 아저씨는 그냥 평범하게 boost::shared_ptr을 쓸 것이지... --; )

개인적으로 나중에 다시 보거나 누군가가 댓글을 달아주겠거니 하는 희망을 갖고

포스팅한다...

[CODE type="c"]
template <typename T>
class CountedPtr
{
private :
  class Impl
  {
  public :
      Impl ( T* pp ) : p ( pp ), refs ( 1 ) { }
      ~Impl ( ) { delete p; }

      T*        p;
      size_t    refs;
  };
  Impl*    impl_;
public :
  explicit CountedPtr ( T* p ) : impl_ ( new Impl ( p ) ) { }
  ~CountedPtr ( ) { Decrement ( ); }

  CountedPtr ( const CountedPtr& other ) : impl_ ( other.impl_ )
  {
      Increment ( );
  }

  CountedPtr& operator = ( const CountedPtr& other )
  {
      if ( impl_ != other.impl_ )
      {
          Decrement ( );     // 얘의 의미가 몰까???
          impl_ = other.impl_;
          Increment ( );
      }

      return *this;
  }

  T* operator -> ( ) const
  {
      return impl_->p;
  }

  T& operator * ( ) const
  {
      return *( impl_->p );
  }

private :
  void Decrement ( )
  {
      if ( --(impl_->refs) == 0 )
          delete impl_;
  }

  void Increment ( )
  {
      ++(impl_->refs);
  }
};
[/HTML][/CODE]

예가 잘못된 것인가??? 내가 잘못 생각한 것인가...
크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by nemonandes

link to tattertools link to Daum link to Tistory link to AllBlog link to DNSever
BLOG main image
may the force be with you................ by nemonandes
mail to nemonandes

카테고리

분류 전체보기 (225)
C落書Log (135)
S關心事Log (74)
C自動車Log (4)
C寫眞Log (12)
Statistics Graph