C++ 重载输入操作>&燃气轮机;在标题或.cpp中
如果我想重载操作符“>>”,这是我的.h文件中的行C++ 重载输入操作>&燃气轮机;在标题或.cpp中,c++,operator-overloading,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; }
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 &operator >> (istream& input,const Money&m2){}
如果将其放在标题中,函数定义中的任何更改都需要重新编译包含它的任何文件。如果您在
.cpp
文件中定义了它,那么您就不需要了,链接器会选择调用它。我不知道是什么困扰着您,所以请使用此示例,看看它是否消除了您的疑问。在此处运行:
.h
#include <iostream>
#include <istream>
using namespace std;
class A{
int p;
public:
friend istream & operator >> (istream&,A&);
friend ostream & operator << (ostream&,A&);
};
输入流操作符引用非常量
std::istream
,并引用要读取数据的非常量对象。你可以把它定义为类的一个朋友,用于效率(直接访问成员变量),但是如果你已经提供了设置这些值的高效机制,你可能想考虑它是否需要成为一个朋友。
在下面的示例中,我定义了一个类Money
,它表示一些值(作为双精度浮点值,这很糟糕,但只是一个示例)和ISO货币代码(作为std::string)。然后,我定义了一个输入流操作符,它读取格式为“13.99 GBP”的输入,以及一个输出流操作符,它以相同的格式写入值
实例:
货币.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::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;
}
注意:当您试图通过const-reference进行访问时,您会发现写入对象的成员是一项挑战。如果您保存在
.h
文件中,这是正常的,但如果保存在.cpp
文件中,这将是一件好事,实际上,您应该保留在.cpp
中,因为这是标准,但不是必需的。我在看一个像这样的例子,我也被教导这样做。但我似乎无法让它工作,因为每次编译它都需要一个参数。@rupshyadav。我的函数名和参数应该是什么?@user3249265。检查我的答案可能会有帮助。
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;
}
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;
}