C++ 'this' 指针在类方法中的使用

标签 c++ oop this binary-tree

我正在将一个二叉树实现从 C 移植到 C++,并在此过程中将其转换为一个类。
对于 C 处理大多数事情的方式,提前表示抱歉。
属性包括

T data;
node<T> *left, *right;

我正在尝试通过以下方式对根节点进行更改

node<T>* current = this;        // points to root node
node<T> newnode = node<T>(5);   // just a test value
current->left = &newnode;

cout << "current->left: " << current->left << " value: " << current->left->data << endl;
cout << "this->left: " << " value: " << this->left->data << endl;

在我看来,这两个打印应该打印完全相同的东西,因为 current 和 this 都指向同一个对象,但我得到的输出明显不同

current->left: 0x7fffffffddb0 value: 5
this->left: 0x7fffffffddb0 value: -139656192

所以他们指向同一个左边的对象,但是当从不同的角度看时,那个对象拥有不同的值,这是怎么回事?


附加信息
声明

template <typename T>
class node {
public:
    T data;
    node<T> *left, *right;

    void insert(T data);
    int remove(T target);  // returns success or not
    node<T>* find(T target);
    void print(int mode);  // need overloading since can't use 'this' as default var
    void print(int mode, node<T>* root);

private:
    node<T>* node_new(T data);
    void node_find(T key, node<T>*** target_node_address_handle);
    void node_delete(node<T>** target_address);
};

构造函数

template <typename T>
node<T>::node(T rootdata) {
    data = rootdata;
    left = NULL;
    right = NULL;
}

查找插入和删除共享的方法

template <typename T>
void node<T>::node_find(T key, node<T>*** target_node_address_handle) {
    node<T>* current = this;
    while(current) {
         if (typeid(key) == typeid(current->data)) {
              if (key == current->data) break;
              else if (key < current->data) current = current->left;
              else current = current->right;
         }
    }
    // if loop exited without breaking, will insert into empty NULL position
    *target_node_address_handle = &current;
}

我的插入不起作用,但我不确定这是不是 node_find 或 node_insert 的问题

template <typename T>
void node<T>::insert(T data) {
    node<T>** target_node_address;
    node_find(data, &target_node_address);  
    // target_node_address should now point to one of root's decedents

    if (!(*target_node_address))   // nothing at target node as expected
        *target_node_address = node_new(data);   
        // node_new returns pointer to node
}

编辑
拷贝构造函数

template <typename T>
node<T>::node(const node<T>& anothernode) {
    data = anothernode.data;
    if(anothernode.left) *left = *anothernode.left;
    else left = NULL;

    if(anothernode.right) *right = *anothernode.right;
    else right = NULL;
}

析构函数(都在声明中添加)

template <typename T>
node<T>::~node(void) {
    left = NULL;
    right = NULL;
}

有趣的是,在我明确添加这些之前它编译得很好......

最佳答案

新节点解决的问题和插入更改为:

template <typename T>
node<T>* node<T>::node_new(T data) {
    node<T>* newnode = new node<T>(data);
    return newnode;
}

template <typename T>
void node<T>::node_find(T key, node<T>*** target_node_address_handle) {
    // find node matched by key, or NULL pointer in correct location
    // give node pointer address back
    node<T>* root = this;
    node<T>** target_address = &root;
    while(*target_address) {
        node<T>* current = *target_address;
        if(typeid(key) == typeid(current->data)) {  
            //  assume comparison operator exists
            if(key == current->data)
                break;
            else if(key < current->data)
                target_address = &current->left;
            else
                target_address = &current->right;
        }
    }
    // if loop exited without breaking, will insert into an empty NULL position
    // else loop exited by matching/breaking, will delete non-NULL node
    *target_node_address_handle = target_address;
}

问题是我将 ¤t 返回给 *target_node_address_handle,这将是指向 NULL 指针的 address,而不是 < b>pointer 指向 NULL 的指针,我可以更改它。

关于C++ 'this' 指针在类方法中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24719414/

相关文章:

javascript - 鼠标悬停时淡入/淡出图片

javascript - typescript :为什么不将 'this' 视为 Javascript?

c++ - static int 执行了多少次?

java - oop:理解类之间的依赖和耦合

php - 对象构造函数和唯一 ID

c# - 对服务类使用静态只读引用有什么问题吗?

javascript - 使用 JavaScript "this"更改文本框中的文本?

c++ - 为什么我的 ifstream 中出现无限循环?

c++ - 如何将重复元素插入 vector 中?

c++ - 在 C++ 动态链接函数中调用 Octave 排序?