问题描述
我正在读一本关于二叉搜索树的书,有些奇怪的东西出现了。
class BST
{
public:
void insert(const Comparable& item)
private:
BinaryNode * root;
struct BinaryNode
{
可比较的元素;
BinaryNode * left;
BinaryNode * right;
BinaryNode(const Comparable& theElement,BinaryNode * lt,BinaryNode * rt):
element(theElement),left(lt),right(rt){}
}
void insert(const Comparable& item,BinaryNode *& t)const;
};
私人
插入
函数是 public
插入
函数的帮助函数, private
insert
函数寻找正确的地方使用递归插入。
我不明白是不是 BinaryNode *& t
。这是什么意思?
t
的地址的指针
您的表达式 BinaryNode *& t)
BinaryNode *& t
------------- -----
二进制节点指针t是引用变量
因此 t
是引用 BinaryNode 类的指针。
你在c ++中混淆了 ampersand&
运算符。给出变量的地址。但语法不同。
&
前面有一些变量,如下所示:
BinaryNode b;
BinaryNode * ptr =& b;但是下面的方法是对于引用变量(它的简单的非指针):
b
BinaryNode b;
BinaryNode& t = b;
,如下所示:
BinaryNode b;
BinaryNode * ptr =& b;
BinaryNode *& t = ptr;
I am reading a book about Binary Search Tree and something weird came up.
class BST
{
public:
void insert(const Comparable & item)
private:
BinaryNode *root;
struct BinaryNode
{
Comparable element;
BinaryNode *left;
BinaryNode *right;
BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt) :
element(theElement), left(lt), right(rt) {}
}
void insert(const Comparable & item, BinaryNode * & t) const;
};
The private
insert
function is helper function for public
insert
function, and private
insert
function looks for the right place to insert using recursion.
Part that I don't understand is BinaryNode * & t
in the parameter. What does it mean?Pointer of the address of t
?
解决方案 In your expression BinaryNode * & t)
BinaryNode* & t
------------- -----
BinaryNode pointer t is reference variable
so t
is reference to pointer of BinaryNode class.
You are confused ampersand &
operator in c++. that give address of an variable. but syntax is different.
ampersand &
in front of some of variable like below:
BinaryNode b;
BinaryNode* ptr = &b;
But following way is for reference variable (its simple not pointer):
BinaryNode b;
BinaryNode & t = b;
and your is like below:
BinaryNode b;
BinaryNode* ptr = &b;
BinaryNode* &t = ptr;
这篇关于在参数c ++中的星号和和号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!