Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

pointers - both asterisk and ampersand in a parameter c++

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your expression BinaryNode * & t)

            BinaryNode*                & t
           -------------              -----
            BinaryNode pointer        t is reference variable  

so t is reference to pointer of BinaryNode class.

Pointer of the address of t?

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;  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...