both asterisk and ampersand in a parameter c++

前端 未结 2 612
独厮守ぢ
独厮守ぢ 2020-12-05 18:54

I am reading a book about Binary Search Tree and something weird came up.

class BST
{
public:
   void insert(const Comparable & item)

private:
   Binary         


        
2条回答
  •  悲&欢浪女
    2020-12-05 19:34

    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;  
    

提交回复
热议问题