問題描述
我正在讀一本關于二叉搜索樹的書,然后出現了一些奇怪的事情.我在函數參數中遇到了以下聲明.
I am reading a book about Binary Search Tree and something weird came up. I came across the following declaration in a function parameter.
BinaryNode * & t
什么意思?t
的地址指針?
What does it mean? Pointer of the address of t
?
對于上下文,這是我看到的代碼.私有insert
函數是公共insert
函數的輔助函數,私有insert
函數使用遞歸尋找合適的插入位置.>
For context, this is the code where I saw this. The private insert
function is a helper function for public insert
function, and private insert
function looks for the right place to insert using recursion.
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;
};
推薦答案
在你的表達式中 BinaryNode * &t)
BinaryNode* & t
------------- -----
BinaryNode pointer t is reference variable
so t
是對 BinaryNode 類的指針的引用.
so t
is reference to pointer of BinaryNode class.
t 的地址指針?
您對 C++ 中的 &
運算符感到困惑.給出一個變量的地址.但語法不同.
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;
你的如下:
BinaryNode b;
BinaryNode* ptr = &b;
BinaryNode* &t = ptr;
這篇關于參數中的星號和與號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!