C++Friend语法/语义问题

C++ Friend Syntax/Semantics Question

本文关键字:问题 语义 语法 C++Friend      更新时间:2023-10-16

我的代码是在以下时间审阅的:https://codereview.stackexchange.com/questions/3754/c-script-could-i-get-feed-back/3755#3755

使用了以下内容:

class Point
{
    public:
    float   distance(Point const& rhs) const
    {
        float dx    = x - rhs.x;
        float dy    = y - rhs.y;
        return sqrt( dx * dx + dy * dy);
    }
    private:
        float   x;
        float   y;
        friend std::istream& operator>>(std::istream& stream, Point& point)
        {
            return stream >> point.x >> point.y;
        }
        friend std::ostream& operator<<(std::ostream& stream, Point const& point)
        {
            return stream << point.x << " " << point.y << " ";
        }
};

由另一成员。我不明白朋友功能在做什么。有没有其他方法可以做到这一点,而不让他们成为朋友功能?当它们是私有的时,客户端如何使用以下方法访问它们?有人能解释一下究竟是什么被归还了吗?

int main()
{
    std::ifstream       data("Plop");
    // Trying to find the closest point to this.
    Point   first;
    data >> first;
    // The next point is the closest until we find a better one
    Point   closest;
    data >> closest;
    float   bestDistance = first.distance(closest);
    Point   next;
    while(data >> next)
    {
        float nextDistance  = first.distance(next);
        if (nextDistance < bestDistance)
        {
            bestDistance    = nextDistance;
            closest         = next;
        }
    }
    std::cout << "First(" << first << ") Closest(" << closest << ")n";
}

当它们是私有的时,客户端如何使用以下方法访问它们?

是的。由于friend函数不是类的成员,因此在哪里定义或声明它们并不重要。任何人都可以使用它们。访问规则不适用于它们。

有人能解释一下究竟是什么被归还了吗?

CCD_ 2返回作为对输入流的引用的CCD_。并且operator<<()返回作为输出流的参考的std::ostream&

有没有其他方法可以做到这一点,而不让他们成为朋友功能?

是的。有办法。您可以将两个成员函数inputoutput添加到类的public部分,这将执行friend函数现在正在执行的操作,并且您可以使operator<<operator>>成为非友元函数,如下所示:

class Point
{
    public:
    //....
    std::istream& input(std::istream& stream)
    {
       return stream >> point.x >> point.y;
    }
    std::ostream& output(std::ostream& stream) const
    {
       return stream << point.x << " " << point.y << " ";
    }
    //...
};
std::istream& operator>>(std::istream& stream, Point& point)
{
  return point.input(stream);
}
std::ostream& operator<<(std::ostream& stream, Point const& point)
{
  return point.output(stream);
}

您可以在没有友元函数的情况下通过为X和Y成员变量定义"getters"和适当的构造函数来实现这一点,如

class Point
{
public:
  Point(float xx, float yy) : x(xx), y(yy) {}
  float getX() const { return x; }
  float getY() const { return y; }
private:
  float x;
  float y;
};
std::istream& operator>>(std::istream& stream, Point& point)
{
  float x, y;
  stream >> x >> y;
  point = Point(x, y);
  return stream;
}
std::ostream& operator<<(std::ostream& stream, const Point& point)
{
  return stream << point.getX() << " " << point.getY() << " ";
}

任你选择,两者都有效。

friend函数独立于类,但允许访问私有成员。

在您的类中,无法访问xy成员(顺便说一句,这使该类有点无用(,因此为了能够处理流实例的读/写,这些函数必须声明为友元。

如果你以前从未见过friend概念,那么这可能意味着你正试图通过编写代码来自学C++。这是一个可怕的想法。。。由于许多不同的原因,C++不能以这种方式学习。

挑选一本好书,一本接一本地读,然后进行实验。这是迄今为止最快(唯一(的方法。

你有多聪明并不重要(事实上,你越聪明,通过实验学习C++就越困难:逻辑在这个地方并不总是有帮助(。