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

时间:2014-07-13 03:58:51

标签: 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->left: 0x7fffffffddb0 value: 5
this->left: 0x7fffffffddb0 value: -139656192

所以他们指向相同的左对象,但是当以不同的方式查看时,该对象包含不同的值,是什么?


Additoinal Info
声明

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;
}

有趣的是,在我明确地添加这些内容之前,它已编译好......

1 个答案:

答案 0 :(得分:0)

新节点解决了问题,插入更改为:

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;
}

问题是我将&current返回*target_node_address_handle,这是指向NULL的地址,而不是指针到指向NULL的指针,我可以改变它。