c++ - 对象中this指针“丢失”?
天蓬老师
天蓬老师 2017-04-17 15:18:54
0
1
601

在调试程序的时候发现,在调用对象方法的时候,其this指针指向的地址明显有误,导致无法使用其成员。
头文件:

#pragma once
#include<string>
#include<vector>
#include<iostream>
using namespace std;
/*
公有:构造,拷贝,析构,寻找最大(小)项,判断指定元素是否包含,判断是否为空,遍历打印,做空,插
入指定元素,删除指定元素,重载对于二叉树的=操作。
私有:结点结构,结点指针类型的root成员,拷贝结点,以及除去构造拷贝析构和=重载以外所有的共有方法。
解析:所有的公有中用于二叉树的具体方法,都是依靠递归调用私有方法来实现的。因为二叉树是以结点为最小
操作单元依靠指针链接的对象,所以针对二叉树的操作只能依靠对结点的操作然后递归遍历。
*/
template <typename T>
class BinarySearchTree
{
public:
    BinarySearchTree(typename vector<T>::iterator begin, const typename vector<T>::iterator end);
    //BinarySearchTree(const BinarySearchTree&);
    ~BinarySearchTree() { makeEmpty(); }

    const T& findMax() const 
    {
        Node* t = findMax(root);
        return t->element;
    }
    const T& findMin() const
    {
        Node* t = findMin(root);
        return t->element;
    }
    bool contains(const T& x) const { return contains(x, root); }
    bool isEmpty() const { return isEmpty(root); } //this不明
    void printTree() const { printTree(root); }

    void makeEmpty() { makeEmpty(root); }
    void insert(const T&);
    void remove(const T& x) { remove(x, root); }
    //const &BinarySearchTree operator=(const &BinarySearchTree rhs);

private:
    struct Node
    {
        T element;
        Node* left;
        Node* right;

        Node(const T& theElement,Node* lt,Node* rt) : element(theElement),
            left(lt),right(rt) { }
    };
    Node* root;

    Node* findMax(Node*) const;
    Node* findMin(Node*) const;
    bool contains(const T&, Node*) const;
    bool isEmpty(const Node*) const;
    void printTree(Node* t) const;

    void makeEmpty(Node*&);
    void insert(const T&, Node*&);
    void remove(const T&, Node*&);
    Node* clone(Node* t) const;
};

template<typename T>
typename BinarySearchTree<T>::Node* BinarySearchTree<T>::findMax(Node* t) const
{
    if (t == nullptr)
        return t;
    if (t->right == nullptr)
        return t;
    return findMax(t->right);
}

template<typename T>
typename BinarySearchTree<T>::Node * BinarySearchTree<T>::findMin(Node* t) const
{
    if (t == nullptr)
        return t;
     if (t->left == nullptr)
        return t;
    return findMin(t->left);
}

template<typename T>
bool BinarySearchTree<T>::contains(const T& x, Node* t) const
{
    if (t == nullptr)
        return false;
    else if (t->element > x)
        return contains(x, t->left);
    else if (t->element < x)
        return contains(x, t->right);
    else
    return true;
}

template<typename T>
inline bool BinarySearchTree<T>::isEmpty(const Node * t) const
{
    if (t == nullptr)
        return false;
    else
        return true;
}

template<typename T>
inline BinarySearchTree<T>::BinarySearchTree
(typename vector<T>::iterator begin, const typename vector<T>::iterator end)
{
    while (begin != end)
    {
        insert(*begin, *&root);
        ++begin;
    }
}

template<typename T>
void BinarySearchTree<T>::insert(const T& x, Node*& t)
{
    if (t == nullptr)
        t = new Node(x, nullptr, nullptr);
    else if (t->element > x)
        return insert(x, t->left);
    else if (t->element < x)
        return insert(x, t->right);
    else
        ;
}

template<typename T>
void BinarySearchTree<T>::printTree(Node * t) const
{
    if (isEmpty())              
    {
        cout << "null tree" << endl;
        return;
    }
    if (t != nullptr)
    {
        printTree(t->left);
        cout << t->element <<" ";
        printTree(t->right);
    }
    return;
}

template<typename T>
void BinarySearchTree<T>::makeEmpty(Node *& t)
{
    if (t != nullptr)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t = nullptr;
}



template<typename T>
inline void BinarySearchTree<T>::remove(const T& x, Node*& t)
{
    if (t == nullptr)
        return;
    if (x < t->element)
        return remove(x, t->left);
    else if (x > t->element)
        return remove(x, t->right);
    else if (t->left != nullptr && t->right != nullptr)
    {
        t->element = findMin(t->right)->element;
        remove(t->element, t->right);
    }
    else
    {
        Node * oldNode = t;
        t = (t->left != nullptr) ? t->left : t->right;
        delete oldNode;
    }
}

template<typename T>
typename BinarySearchTree<T>::Node * BinarySearchTree<T>::clone(Node * t) const
{
    
    return NULL;
}

cpp文件
 #include"BinarySearchTree.h"
#include<vector>
#include<iostream>
using namespace std;

int main()
{
    vector<int> vec = {13,2,4,43,5,6,1};
    BinarySearchTree<int> bst(vec.begin(), vec.end());
    bst.printTree();
    return 0;
    

可以看出在133行将要调用isEmpty()的时候,this指针还是正确的,但是一旦调用之后到了32行this就“丢失了”,导致无法读取root变量的内存。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
小葫芦

thisNo problem, that’s just because the debugger hasn’t entered the isEmpty function yet, so this is not displayed.
You split the single line into multiple lines

bool isEmpty() const { 
    return isEmpty(root);  // 让调试器在这行break
} //this不明

In the return linebreak, the address of this will reappear

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template