重载流运算符的正确方法是什么<< >>>为我的班级?

发布于 2024-08-23 09:46:09 字数 266 浏览 4 评论 0 原文

我对如何在 C++ 中重载我的类的流运算符有点困惑,因为它们似乎是流类上的函数,而不是我的类上的函数。执行此操作的正常方法是什么?目前,对于“获取”运算符,我有一个

istream& operator>>(istream& is, Thing& thing) { // etc...

有效的定义。 Thing 类的定义中没有提到这一点。我希望它能够在其实现中访问我的 Thing 类的成员 - 我该如何做到这一点?

I'm a bit confused about how to overload the stream operators for my class in C++, since it seems they are functions on the stream classes, not on my class. What's the normal way to do this? At the moment, for the "get from" operator, I have a definition

istream& operator>>(istream& is, Thing& thing) { // etc...

which works. It's not mentioned in the definition of the Thing class. I want it to be able to access members of my Thing class in its implementation - how do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

败给现实 2024-08-30 09:46:09

你的实现很好。您需要执行的唯一额外步骤是在 Thing 中将您的操作员声明为 friend

class Thing {
public:
  friend istream& operator>>(istream&, Thing&);
  ...
}

Your implementation is fine. The only additional step you need to perform is to declare your operator as a friend in Thing:

class Thing {
public:
  friend istream& operator>>(istream&, Thing&);
  ...
}
但可醉心 2024-08-30 09:46:09

其他答案都是对的。如果它对您有帮助,这里有一个代码示例():

class MyClass {
  int x, y;
public:
  MyClass(int i, int j) { 
     x = i; 
     y = j; 
  }
  friend ostream &operator<<(ostream &stream, MyClass ob);
  friend istream &operator>>(istream &stream, MyClass &ob);
};

ostream &operator<<(ostream &stream, MyClass ob)
{
  stream << ob.x << ' ' << ob.y << '\n';

  return stream;
}

istream &operator>>(istream &stream, MyClass &ob)
{
  stream >> ob.x >> ob.y;

  return stream;
}

The other answers are right. In case it helps you, here's a code example (source):

class MyClass {
  int x, y;
public:
  MyClass(int i, int j) { 
     x = i; 
     y = j; 
  }
  friend ostream &operator<<(ostream &stream, MyClass ob);
  friend istream &operator>>(istream &stream, MyClass &ob);
};

ostream &operator<<(ostream &stream, MyClass ob)
{
  stream << ob.x << ' ' << ob.y << '\n';

  return stream;
}

istream &operator>>(istream &stream, MyClass &ob)
{
  stream >> ob.x >> ob.y;

  return stream;
}
忘羡 2024-08-30 09:46:09

您使您的 operator>> 成为 Thing 类的友元。

You make your operator>> a friend of the Thing class.

屋顶上的小猫咪 2024-08-30 09:46:09

有多种方法,正确的方法实际上取决于班级的内容。

通常,拥有允许读取信息的公共 API 是有意义的,在这种情况下,流媒体运营商不需要访问私有 API。

也许最流行的方法是将流函数声明为类的友元。

我最喜欢的是提供一个公共的 Boost.Serialization 风格模板函数,它可以用于任何一种方式的流式传输,也可以用于其他用途。

There are several approaches and the right one really depends on what the class does.

Quite often it makes sense to have public API that allows reading the information, in which case the streaming operators do not need to access privates.

Probably the most popular approach is declaring the streaming functions friends of the class.

My favorite is providing a public Boost.Serialization style template function that can be used for streaming either way, as well as for other things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文