开发者

Variable declared as Node *&ptr = root->mRight

I am trying to understand what that means I know that declares a variable to an address of type node and that & gets the address of a variable. I also know that in a function parameter it is a call by reference pointer. But I have never 开发者_如何转开发seen it in a variable declaration before... What does it mean exactly

Node *&ptr = root->mRight

I know about working with pointers and everything I am mostly asking about the Node *& variable type. Thanks!


On the right, root is a pointer to a node. root->mRight is a member of that node, namely its mright, which is another pointer to a node.

On the left, we declare something (before assigning a value to it with =). If it were Node *ptr, it would be a pointer to a node, but because of that &, it is not such a thing in its own right, it is only a reference, an alias of another such thing that already exists. So ptr is just another name for the pointer root->mRight.


To understand this --> Node *&ptr = root->mRight you compare it with this

int a =5;
int & al = a;

here "al" is just another name of "a"

now replace

int -> Node *
a   -> root->mright
ptr ->al

    

so you will understand that here "mRight" is a pointer to Node and ptr is just another name of "mRight".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜