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

因此,我正在查看一些与我在学校上班的项目有关的代码,我发现了一个函数实现,该函数实现在返回值之前具有私有属性,我希望有人可以解释其目的和用途,我。我一直无法在线找到有关它的任何信息,可能是因为我不确定如何在不将其重定向到类定义或基本函数定义中有关private的信息的情况下提出问题。

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,val);

   if(isRed(h.right))
      h = rotateLeft(h);

   if(isRed(h.left) && isRed(h.left.left))
      h = rotateRight(h);

   return h;
}

这是关于左倾的红黑树。 预先感谢。

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

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

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

请参见What is the difference between public,protected,package-private and private in 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,value);
   else h.right = insert(h.right,value);
   if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h);
   if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h);
   return h;
 }
}
本文链接:https://www.f2er.com/3052391.html

大家都在问