简体   繁体   English

返回类型“BST&”是什么意思?

[英]What is the return type "BST&" meaning?

I tried to implement a simple BST with a member of class that insert a value into the tree.我尝试使用将值插入树中的类成员来实现一个简单的 BST。 I followed the snipping code like below:我按照如下截图代码:

class BST {
public:
    int value;
    BST* left;
    BST* right;

    BST(int val);
    BST& insert(int val);
};

But I don't understand what the return type BST& means.但我不明白返回类型BST&是什么意思。 What does it mean ?这是什么意思 ?

This is a reference to an instance of BST.这是对 BST 实例的引用。 It means that you can change the BST through that reference, eg:这意味着您可以通过该引用更改 BST,例如:

BST tree(1);
BST &newNode = tree.insert(3);
newNode.value = 4;

The code snippet above will create a tree with value 1, then insert a node with value 3, but replace that value with 4 in the third line.上面的代码片段将创建一个值为 1 的树,然后插入一个值为 3 的节点,但在第三行中将该值替换为 4。

For more information about C++ references, consider reading:有关 C++ 引用的更多信息,请考虑阅读:

What are the differences between a pointer variable and a reference variable in C++? C++中的指针变量和引用变量有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM