[英]What does private in the scope of the return value of a function definition/implementation mean (c++)?
所以我正在查看我發現的一些與我正在為學校工作的項目相關的代碼,我發現了一個 function 實現,它在返回值之前是私有的,我希望有人可以向我解釋它的目的和用途。 我無法在網上找到任何關於它的信息,可能是因為我不完全確定如何在不被重定向到 class 定義或基本 function 定義中的私有信息的情況下提出問題。
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;
}
這是關於左傾紅黑樹的。 提前致謝。
我剛剛搜索了您的代碼並在https://www.cs.princeton.edu/~rs/talks/LLRB/LLRB.pdf第 5 頁中找到了它
這是 java 代碼是 class 實現的一部分。 所以 private 只是將此方法聲明為私有,這意味着只能從 class 內部調用此方法。
請參閱Java 中的 public、protected、package-private 和 private 有什么區別?
我不確定您的文檔是什么樣子,但該文檔明確指出 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.