return 类型 "BST&" 是什么意思?

What is the return type "BST&" meaning?

我尝试用 class 的成员实现一个简单的 BST,将一个值插入到树中。我按照下面的截图代码进行操作:

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

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

但是我不明白 return 类型 BST& 是什么意思。这是什么意思?

这是对 BST 实例的引用。这意味着您可以通过该引用更改 BST,例如:

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

上面的代码片段将创建一个值为 1 的树,然后插入一个值为 3 的节点,但在第三行将该值替换为 4。

有关 C++ 参考的更多信息,请考虑阅读:

What are the differences between a pointer variable and a reference variable in C++?