参数中的星号和与号

2023-02-22C/C++开发问题
3

本文介绍了参数中的星号和与号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在读一本关于二叉搜索树的书,然后出现了一些奇怪的事情.我在函数参数中遇到了以下声明.

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;  

这篇关于参数中的星号和与号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6