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

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

#pragma once #include #include #include using namespace std; /* 公有:构造,拷贝,析构,寻找最大(小)项,判断指定元素是否包含,判断是否为空,遍历打印,做空,插 入指定元素,删除指定元素,重载对于二叉树的=操作。 私有:结点结构,结点指针类型的root成员,拷贝结点,以及除去构造拷贝析构和=重载以外所有的共有方法。 解析:所有的公有中用于二叉树的具体方法,都是依靠递归调用私有方法来实现的。因为二叉树是以结点为最小 操作单元依靠指针链接的对象,所以针对二叉树的操作只能依靠对结点的操作然后递归遍历。 */ template  class BinarySearchTree { public: BinarySearchTree(typename vector::iterator begin, const typename vector::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 BinarySearchTree::Node* BinarySearchTree::findMax(Node* t) const { if (t == nullptr) return t; if (t->right == nullptr) return t; return findMax(t->right); } template typename BinarySearchTree::Node * BinarySearchTree::findMin(Node* t) const { if (t == nullptr) return t; if (t->left == nullptr) return t; return findMin(t->left); } template bool BinarySearchTree::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 inline bool BinarySearchTree::isEmpty(const Node * t) const { if (t == nullptr) return false; else return true; } template inline BinarySearchTree::BinarySearchTree (typename vector::iterator begin, const typename vector::iterator end) { while (begin != end) { insert(*begin, *&root); ++begin; } } template void BinarySearchTree::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 void BinarySearchTree::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 void BinarySearchTree::makeEmpty(Node *& t) { if (t != nullptr) { makeEmpty(t->left); makeEmpty(t->right); delete t; } t = nullptr; } template inline void BinarySearchTree::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 BinarySearchTree::Node * BinarySearchTree::clone(Node * t) const { return NULL; } cpp文件 #include"BinarySearchTree.h" #include #include using namespace std; int main() { vector vec = {13,2,4,43,5,6,1}; BinarySearchTree bst(vec.begin(), vec.end()); bst.printTree(); return 0;

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

天蓬老师
天蓬老师

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

全部回复 (1)
小葫芦

this没问题,那只是因为调试器还没进入isEmpty函数,this不显示而已,
你把单行拆成多行

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

return那行breakthis的地址就会重新出现了

    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责声明 Sitemap
    PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!