私有在函数定义/实现的返回值范围内是什么意思 (c++)?

What does private in the scope of the return value of a function definition/implementation mean (c++)?

本文关键字:意思 是什么 范围内 c++ 返回值 函数 定义 实现      更新时间:2023-10-16

所以我正在查看我发现的与我正在为学校工作的项目相关的一些代码,我发现了一个在返回值之前具有私有的函数实现,我希望有人可以向我解释它的目的和用途。我无法在网上找到有关它的任何信息,可能是因为我不完全确定如何在不被重定向到有关私有类定义或基本函数定义的信息的情况下提出问题。

private Node insert(Node h, Key key, Value val)
{
if(h == null)
return new Node(key, val, RED);
if(isRed(h.left) && isRed(h.right))
colorFlip(h);
int cmp = key.compateTo(h.key);
if(cmp == 0) h.val = val;
else if(cmp < 0)
h.left = insert(h.left, key, val);
else
h.right = insert(h.right, key, val);
if(isRed(h.right))
h = rotateLeft(h);
if(isRed(h.left) && isRed(h.left.left))
h = rotateRight(h);
return h;
}

这是关于左倾的红黑树。 提前谢谢。

我刚刚用谷歌搜索了你的代码,并在第 5 页中找到 https://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf

这是java,代码是类实现的一部分。所以 private 只是将此方法声明为 private,这意味着此方法只能从类中调用。

请参阅 Java 中的公共、受保护、包私有和私有之间有什么区别?

我不确定您的文档是什么样子的,但该文档清楚地指出该实现是用 Java 提供的。

private class Node
{
private Key key;
private Value val;
private Node left, right;
private boolean color;
Node(Key key, Value val)
{
this.key = key;
this.val = val;
this.color = RED;
}
}
public Value search(Key key)
{
Node x = root;
while (x != null)
{
int cmp = key.compareTo(x.key);
if (cmp == 0) return x.val;
else if (cmp < 0) x = x.left;
else if (cmp > 0) x = x.right;
}
return null;
}
public void insert(Key key, Value value)
{
root = insert(root, key, value);
root.color = BLACK;
}
private Node insert(Node h, Key key, Value value)
{
if (h == null) return new Node(key, value);
if (isRed(h.left) && isRed(h.right)) colorFlip(h);
int cmp = key.compareTo(h.key);
if (cmp == 0) h.val = value;
else if (cmp < 0) h.left = insert(h.left, key, value);
else h.right = insert(h.right, key, value);
if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h);
if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
return h;
}
}