2013-12-19 96 views
-1

我已經構建了一個旨在檢查二叉樹的迭代器類。這個迭代器波紋管所:C++在迭代器類中定義析構函數?

template <class Key> class intervalST_const_iterator 
{ 
//friend class IntervalST<Key>; 

private: 
    const Interval<Key> *interval;// equivalent to Interval<Key> const *interval; meaning: interval is apointer to the const object: Interval<Key> 
            //meaning that the object cannot be changed via interval 
            //interval can change here 

public: 
    intervalST_const_iterator(const Interval<Key> *p): interval(p){} 
    //~intervalST_const_iterator() {delete interval;} 
    //the const at the end of this operator means that the operator will not change *this 
    bool operator != (const intervalST_const_iterator & other) const 
    { 
     return this->interval != other.interval; 
    } 
    bool operator == (const intervalST_const_iterator & other) const 
    { 
     return this->interval == other.interval; 
    } 
    //the const at the beginning means that the it's not allowed to change the object 
    const Interval<Key> operator *() const 
    { 
     return *(this->interval); 
    } 
    intervalST_const_iterator<Key> & left() 
    { 
     interval = interval->left; 
     return *this; 

    } 
    intervalST_const_iterator<Key> & right() 
    { 
     interval = interval->right; 
     return *this; 
    } 
}; 

在這個類中,我還定義了一個析構函數~intervalST_const_iterator() {delete interval;}我已經把它放在評論。

爲了一棵樹類,看起來像重複我使用這個迭代器:

template <class Key> class intervalST_const_iterator; 

template <class Key> class IntervalST 
{ 
private: 
    Interval<Key> *root; 

    //friend class intervalST_const_iterator<Key>;//allow the iterator class to access the private section of intervalST 

    bool isRed(Interval<Key> *interval); 
    Interval<Key> *rotateLeft(Interval<Key> *h); 
    Interval<Key> *rotateRight(Interval<Key> *h); 
    Interval<Key> *put(Interval<Key> *h,Key lo, Key hi, Key val); 
    Interval<Key> *moveRedLeft(Interval<Key> *h); 
    Interval<Key> *moveRedRight(Interval<Key> *h); 
    Interval<Key> *deleteMin(Interval<Key> *h, Key hi); 
    Interval<Key> *balance(Interval<Key> *h); 
    Interval<Key> *remove(Interval<Key> *h, Key lo, Key hi); 
    Interval<Key> *min(Interval<Key> *h); 
    Interval<Key> *addDuplicate(Interval<Key> *h, Key hi); 
    Interval<Key> *removeDuplicate(Interval<Key> *h, Key low, Key hi); 
    Interval<Key> *getPointerToKey(Key low); 

    void flipColors(Interval<Key> *h); 
    void destroy(Interval<Key> *h); 
    void printTree(Interval<Key> *h, int indent); 
    Key maxVal(Interval<Key> *h); 
    int size(Interval<Key> *h); 
    bool isBST(Interval<Key> *x, Key min, Key max); 
    inline bool isBST(){return isBST(root,0,0);} 
    bool isSizeConsistent(Interval<Key> *x); 
    inline bool isSizeConsistent(){return isSizeConsistent(root);} 
    bool is23(Interval<Key> *x); 
    inline bool is23(){return is23(root);} 
    bool isBalanced(); 
    bool isBalanced(Interval<Key> *x,int black); 
    int getKeySize(Key low); 
    int compare(Key a, Key b); 

public: 

    //don't forget to build the constructor 
    //and overload the =equal operator 
    typedef intervalST_const_iterator<Key> const_iterator;//const iterator on a tree 


    const_iterator begin() const; 
    const_iterator end() const; 

    IntervalST():root(NULL){}; 
    ~IntervalST(); 
    void remove(Key lo, Key hi); 
    void put(Key lo, Key hi); 
    inline int size(){return size(root);} 
    inline bool isEmpty(){return root == NULL;} 
    void print(int indent = 0); 
    void check(); 


}; 

相應begin()end()方法迭代

template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::begin() const 
{ 
return const_iterator(root); 
} 

template <typename Key> typename IntervalST<Key>::const_iterator IntervalST<Key>::end() const 
{ 
//return const_iterator(NULL,this); 
return const_iterator(NULL); 
} 

然後我使用函數 - 請參閱波紋管 - 當迭代器類的析構函數未定義時運行良好,當析構函數定義爲時,會產生奇怪的結果。

爲什麼發生這種情況? 我是否需要爲迭代器類定義析構函數以防止泄漏? 如何來處理這個問題? 感謝您的幫助。

template <typename Key> std::vector<Segment<Key> > findIntersections(const IntervalST<Key> &tree ,Segment<Key> segment) 
{ 
//initialize a const iterator on the tree 
intervalST_const_iterator<Key> iterator = tree.begin(); 
vector<Segment<Key> > intersections; 

Key k = (*iterator).max; 
//walk the tree 
while(iterator != tree.end()) 
{ 
    //if(*(iterator).intersects(segment.gety1(),segment.gety2())) intersections.push_back(segment); 
    if(intersects((*iterator).low,(*iterator).back,segment.gety1(),segment.gety2())) 
    { 
     intersections.push_back(segment); 
     return intersections; 
    } 
    else if (iterator.left() == tree.end())           iterator = iterator.right(); 
    else if (segment.gety1() > (*iterator.left()).max)        iterator = iterator.right(); 
    else                    iterator = iterator.left(); 
} 
return intersections; 
} 
+0

您可能想了解[三項規則](https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29)。 –

+0

@Joachim Pileborg我認爲問題**沒有鏈接到**我沒有定義一個副本ctor,並且我沒有重載equal(=)運算符。 – PhonoDots

+0

@PhonoDots:確實;這是因爲你刪除了一些你沒有「新」的東西。不要這樣做。 (但是,如果它確實需要析構函數,那麼「三定律」將非常重要)。 –

回答

3

迭代器不擁有Interval,它只是一個指向一個年代由IntervalST它遍歷所有。因此,它不應該刪除它。

迭代器不需要析構函數,也不需要任何other special functions,如果它需要的話。

+0

太好了...非常感謝:-) – PhonoDots