这是我的代码的一部分:
class Node{
public:
Node* parent;
Node* left;
Node* right;
int key;
};
(...)
/*
* Ps.: v1 is parent of v2!
* and assume that v1->parent is NULL
*/
void somefunction(Node*& v1,Node*& v2){
(...)
v2->parent = v1->parent; // The problem is here, more details below
(...)
}
问题是当我调用 时v2->parent,由于 的<type>*&参数somefunction(),编译器解释v2->parent为相等v1(谁是 v2 的父亲),并且它修改v1为NULL,但我只想修改v2->parentvalue 而不是v1。
PS.:我需要<type>*&在代码下面的某些部分使用,我不能只修改参数类型。