C++Friend语法/语义问题
C++ Friend Syntax/Semantics Question
我的代码是在以下时间审阅的: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&
。
有没有其他方法可以做到这一点,而不让他们成为朋友功能?
是的。有办法。您可以将两个成员函数input
和output
添加到类的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函数独立于类,但允许访问私有成员。
在您的类中,无法访问x
和y
成员(顺便说一句,这使该类有点无用(,因此为了能够处理流实例的读/写,这些函数必须声明为友元。
如果你以前从未见过friend
概念,那么这可能意味着你正试图通过编写代码来自学C++。这是一个可怕的想法。。。由于许多不同的原因,C++不能以这种方式学习。
挑选一本好书,一本接一本地读,然后进行实验。这是迄今为止最快(唯一(的方法。
你有多聪明并不重要(事实上,你越聪明,通过实验学习C++就越困难:逻辑在这个地方并不总是有帮助(。
- 警告处理为错误这里有什么问题
- Xcode 语义问题引用或以前定义的代码
- Xcode(版本9.2(9C40B)):试图包括Boost时C 语义和链接器问题
- 由于libkern.h中的语义问题,Xcode Build失败
- 解析问题:预期的不合格的ID和语义问题:C 需要所有声明的类型说明符
- 二进制表达式的语义问题无效操作数("双精度"和"双精度")
- 将 Xcode 项目与 MathGL 链接时的语义问题
- 语义问题:调用'displayBoard'没有匹配函数
- xcode 构建错误:从指针到较小类型"int"的语义问题丢失了信息
- 语义问题 "Attempt to use a deleted function" 遇到使用 std::线程用法编译 c++ 代码
- 成功包含文件,现在处理语义问题
- 重载迭代器:c++语义问题
- 关于对象初始化语义的问题
- C++语法/语义问题:对Function和typedef关键字的引用
- C++Friend语法/语义问题
- 关于声明语义的几个问题
- C++ 语义问题:"'use of undeclared identifier 'balance'"
- Mersenne Twister & 接收算法中的语义问题.h
- C++11 移动语义行为特定问题
- 使用push_back时的语义问题