如何“返回值"?的运算符>>流课程的工作? [英] How does the "return value" of operator >> of istream class work?
问题描述
我试图理解以下说法:
int main() {
fstream inf( "ex.txt", ios::in );
char c;
while( inf >> c ) {
cout << c << ", ";
}
return 0;
}
( inf>> c
)在上面的while循环中返回什么?我下载了gcc源代码并尝试使用它,但是它对我来说太复杂了:(.
What does ( inf >> c
) return in the while loop above? I downloaded the gcc source code and tried to play around with it but it was way too complex for me :( .
我检查了C ++参考页,发现它返回了对自身的引用:
I checked the C++ reference page, I realized that it return a reference to itself:
istream& operator>> (bool& val );
istream& operator>> (short& val );
istream& operator>> (unsigned short& val );
istream& operator>> (int& val );
istream& operator>> (unsigned int& val );
istream& operator>> (long& val );
istream& operator>> (unsigned long& val );
istream& operator>> (float& val );
istream& operator>> (double& val );
istream& operator>> (long double& val );
istream& operator>> (void*& val );
istream& operator>> (streambuf* sb );
istream& operator>> (istream& ( *pf )(istream&));
istream& operator>> (ios& ( *pf )(ios&));
istream& operator>> (ios_base& ( *pf )(ios_base&));
*** the following functions are not members but GLOBAL functions:
istream& operator>> (istream& is, char& ch );
istream& operator>> (istream& is, signed char& ch );
istream& operator>> (istream& is, unsigned char& ch );
istream& operator>> (istream& is, char* str );
istream& operator>> (istream& is, signed char* str );
istream& operator>> (istream& is, unsigned char* str );
所以我创建了一个类似的类,比方说my_istream:
So I created a similar class, let's say my_istream:
struct my_istream {
my_istream& self_ref;
};
int main() {
my_istream mis;
}
编译时出现此错误:
1>c:\users\chan\documents\visual studio 2010\projects\topcoder\topcoder\main.cpp(26): error C2758: 'my_istream::self_ref' : must be initialized in constructor base/member initializer list
但是,我真的不知道在这种情况下应将self_ref初始化为什么?在处理链表时,我了解指向自身的指针,并且我了解C ++中的reference(&
)只是C中伪装的指针形式.但是我无法解释这种情况吗? istream
的内部实现实际上是如何工作的?引用如何被评估为真或假?谢谢!
However, I don't really know what should self_ref be initialized to in this case? I understand a pointer to itself when dealing with linked-list, and I understand that reference( &
) in C++ is just a disguised form of pointer in C. But I can't explain this situation? How does the internal implementation of istream
actually work? How does a reference can be evaluated as true or false? Thanks!
struct my_istream {
my_istream() {
}
my_istream& operator >>( int x ) {
return *this;
}
};
int main() {
my_istream mis;
int x;
while( mis.operator>>( x ) ) {
cout << "--";
}
}
我应该在my_istream类中添加什么才能在while循环中工作?
What should I add to my_istream class in order to work inside the while loop?
推荐答案
要返回自引用,只需 return * this
.您实际上并没有声明内部自引用.您可以 具有引用成员,但是必须在构造函数初始化器列表中对其进行初始化: class Foo {Foo(int& some_int_ref):my_ref_member(some_int_ref)
To return a self ref you just return *this
. You don't actually declare an internal self reference. You can have reference members, but you must initialize them in the constructor initializer list: class Foo { Foo(int& some_int_ref) :my_ref_member(some_int_ref)
istream还有另一个重载来确定其布尔值.
istream has another overload to determine its boolean value.
可链接成员的示例(例如 operator>>
,但在这种情况下只是一个普通函数),并给一个类一个布尔值(尽管后者是一个复杂的主题,值得其自己的主题):
Example of a chainable member (like operator >>
, but in this case just an ordinary function) and giving a class a boolean value (although the latter is a complex subject worthy of its own subject):
#include <iostream>
class X {
bool B;
public:
X() :B(false) { }
X& toggle() { B = !B; return *this; }
operator void*() { return B ? this : 0; }
};
int main()
{
X x;
x.toggle().toggle().toggle();
if (x)
std::cout << "true!" << std::endl;
}
我不想在此答案中深入研究 operator bool
与 operator void *
,但是这个古老的stackoverflow问题应该为您提供良好的参考:href ="https://stackoverflow.com/questions/2145931/why-is-operator-bool-invoked-when-i-cast-to-long">为什么"operator bool()"当我强制转换为"long"时调用?
I didn't want to delve into operator bool
vs operator void *
in this answer, but this old stackoverflow question should give you good references: Why is "operator bool()" invoked when I cast to "long"?
这篇关于如何“返回值"?的运算符>>流课程的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!