c++ - 在 header 或 .cpp 中重载输入操作 >>

标签 c++ operator-overloading

如果我想重载运算符“>>”,这是我的 .h 文件中的行

  friend istream &operator >> (istream& input,const Money&m2);

例如我想要吗

  friend istream &operator >> (istream& input,const Money&m2){
    input >> m2.dollar;
    return input;
}

进入我的头文件或类文件。如果我将它放入我的类文件中,将如何调用该函数?这样的事情可以吗?

  const Money Money::&operator >> (istream& input,const Money&m2)

类名是“Money.cpp”

最佳答案

输入流运算符获取对非常量 std::istream 的引用,以及对要读取数据的非常量对象的引用。您可以将其定义为类的友元以提高效率(直接访问成员变量),但如果您已经提供了用于设置这些值的有效机制,您可能需要考虑是否需要将其定义为根本就是 friend 。

在下面的示例中,我定义了一个类 Money,它表示某个值(作为 double 浮点值,这很糟糕,但这只是一个示例)和 ISO 货币代码(作为 std::string)。然后,我定义一个输入流运算符,以“13.99 GBP”格式读取输入,并定义一个输出流运算符,以相同格式写入值。

实例:http://coliru.stacked-crooked.com/a/d3e24b4fd697f773

money.hpp

class Money {
  public:
    Money(double value, const std::string& code);

    const std::string& currencyCode() const;
    double value() const;

    friend std::istream& operator>>(std::istream&, Money&);
  private:
    double value_;
    std::string code_;
};

std::istream& operator>>(std::istream& in, Money& m);
std::ostream& operator<<(std::ostream& out, const Money& m);

money.cpp

Money::Money(double value, const std::string& code)
     : value_(value), code_(code) {}

const std::string& Money::currencyCode() const {
  return code_;
}

double Money::value() const {
  return value_;
}

std::istream& operator>>(std::istream& in, Money &m) {
  in >> m.value_ >> m.code_;
  return in;
}

std::ostream& operator<<(std::ostream& out, const Money& m) {
  out << m.value() << " " << m.currencyCode();
  return out;
}

需要牢记的一些要点:

  • 一般来说,输出流操作符不必是友元;通常有一种方法可以通过类的公共(public)成员函数来访问所需的信息,而不会损失效率。
  • 输入流操作符只是出于效率原因而成为 friend ;我们可以直接流式传输到成员变量中。
  • 对于输入流运算符,第二个参数(您正在读入的对象)不得为 const - 输入操作会更改正在读入的对象。
  • 对于输出流运算符,第二个参数(要写出的对象)应该是 const - 输出操作不应更改要写出的对象。

如果构造函数执行一些重要的验证(例如,检查 std::string 是否包含有效的 ISO 货币代码),我们不应该通过直接读取成员变量来绕过该验证我们的输入流操作符。相反,我们应该读入本地 double 值和本地字符串,然后构造一个 Money 对象,将验证交给已经编写的构造函数(请参见下面的示例; header 是相同的,除了删除 friend 来自类的声明)。

实例:http://coliru.stacked-crooked.com/a/233ac7c17e51f612

money.cpp(构造函数中的验证)

Money::Money(double value, const std::string& code)
     : value_(value), code_(code) {
 if (code_ != "GBP") throw std::runtime_error("Must be GBP");
}

const std::string& Money::currencyCode() const {
  return code_;
}

double Money::value() const {
  return value_;
}

std::istream& operator>>(std::istream& in, Money &m) {
  double value(0.0);
  std::string code;
  in >> value >> code;
  m = Money(value, code);
  return in;
}

std::ostream& operator<<(std::ostream& out, const Money& m) {
  out << m.value() << " " << m.currencyCode();
  return out;
}

关于c++ - 在 header 或 .cpp 中重载输入操作 >>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28714954/

相关文章:

c++ - 在 C++ 中,给定 A 类中的一个成员函数,我们能否限制它只能访问 B 类,而不让 B 完全友元访问 A?

c++ - C++ 中的运算符重载 + 和 +=

r - 在 R 中,如何将整个命令行放入二元运算符的 sys.call() 中?

c++ - Operator << 重载继承,为什么我从基类而不是子类获取输入?

c++ - pthread 启动函数中的 return() 与 pthread_exit()

c++ - 连接正向和反向 vector

c++ - 如何使用 bazel 在 OS X 上设置 C++ 编译器?

c++ - OpenCV 3.0 中的主动轮廓模型

c++ - `operator delete` 带大小参数和不带大小参数 : which one is chosen when both are available?

c++ - 在类中重载运算符并返回对私有(private)值的引用