2011-03-08 81 views
0
template<class T> 
class auto_ptr2 { 
public: 
    explicit auto_ptr2(T *p = 0): pointee(p) {} 
    template<class U> 
    auto_ptr2(auto_ptr2<U>& rhs): pointee(rhs.release()) {} 
    ~auto_ptr2() { delete pointee; } 
    template<class U> 
    auto_ptr2<T>& operator=(auto_ptr2<U>& rhs) 
    { 
     if (this != &rhs) reset(rhs.release()); 
     return *this; 
    } 
    T& operator*() const { return *pointee; } 
    T* operator->() const { return pointee; } 
    T* get() const { return pointee; } 
    T* release() 
    { 
     T *oldPointee = pointee; 
     pointee = 0; 
     return oldPointee; 
    } 
    void reset(T *p = 0) 
    { 
     if (pointee != p) { 
      delete pointee; 
      pointee = p; 
     } 
    } 
private: 
    T *pointee; 

    //template<class U> friend class auto_ptr2<U>; 
     // Question 1> Why we have to define this friend class 
     // Question 2> I cannot compile this code with above line with VS2010. 
     // Error 1 error C3772: 'auto_ptr2<T>' : invalid friend template declaration 
}; 

謝謝C++ - 爲什麼我們要定義這個朋友模板類

+2

注意,代碼是不正確由於微妙,因爲你沒有定義拷貝構造函數。如果你有'auto_ptr2 p1(new int); auto_ptr2 p2(p1);'編譯器將不會實例化複製操作的模板化構造函數,並且會隱式定義一個執行按位副本的複製構造函數 - 然後當'auto_ptr2'超出範圍時將獲得UB並且您嘗試刪除兩次相同的指針。 – 2011-03-08 08:39:02

+0

注意,註釋行是*朋友聲明*而不是定義。不要把這個不好,我不想破壞這個問題,但我認爲如果我們學習適當的術語並保持語言的統一性會更好。 – 2011-03-08 08:42:56

回答

3

爲什麼我們要定義這個朋友類

我確信你沒有;據我所見,沒有什麼是引用不同模板實例的私有成員。如果複製構造函數或賦值操作符直接操作rhs.pointee,而不是僅調用rhs.release(),則需要它。

我無法使用VS2010編譯上述代碼。

聲明應該是:

template<class U> friend class auto_ptr2;