각종 지침

1. scoped_refptr


사용법:scopedrefptr는 명시적으로만 참조 메타데이터 인터페이스의 유형을 구현할 수 있습니다.
  • 헤드 파일 포함: #include
  • 기본 클래스 상속public base::RefCountedThreadSafe
  • 예: class MySlice:public base::RefCountedThreadSafe {...}
  • 객체에 접두어 필요:
  • scoped_refptr _slice;
  • 원리:
  • 왜 대상이 scopedrefptr? 보십시오. 응용 계수를 1로 추가합니다.
    template 
    class scoped_refptr {
     public:
      scoped_refptr(T* p) : ptr_(p) {
        if (ptr_)
          ptr_->AddRef();
      }
      ~scoped_refptr() {
        if (ptr_)
          ptr_->Release();
      }
     protected:
      T* ptr_;
    };

    Public base:::RefCountedThreadSafe를 상속해야 하는 이유인용 계수 관리를 돕기 위해:
    template  >
    class RefCountedThreadSafe : public subtle::RefCountedThreadSafeBase {
     public:
      void AddRef() {
        subtle::RefCountedThreadSafeBase::AddRef();
      }
      void Release() {
        if (subtle::RefCountedThreadSafeBase::Release()) {
          Traits::Destruct(static_cast(this));
        }
      }
    };
    void RefCountedThreadSafeBase::AddRef() {
      AtomicRefCountInc(&ref_count_);
    }
    

    2. std::unique_ptr


    단일 소유권의 대상.std::unique 사용ptr.주의해야 할 것은, std::uniqueptr가 가지고 있는 수요는 반드시 인용 계수가 아닌 것이며, 무더기에 분배된 대상이어야 한다.

    좋은 웹페이지 즐겨찾기