Why we can use "while(cin >> a)"

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
Yile
New poster
Posts: 17
Joined: Sun Feb 27, 2005 10:36 am
Location: China

Why we can use "while(cin >> a)"

Post by Yile »

I read some manual and find that the operator >> returns *this, the type should be istream&. Then why can we put it in the while statement as a condition?

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Streams have bool operator() (conversion operator to bool) that returns !this->failed().
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

Yile
New poster
Posts: 17
Joined: Sun Feb 27, 2005 10:36 am
Location: China

Post by Yile »

Where is the operator defined? The class hierarchy is as follows:

Code: Select all

basic_istream->basic_ios->ios_base
Which class is it defined in? I'm afraid I can't find it.

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Oh, actually for some reason it's operator void* and not operator bool that's used here. It's a member of basic_ios.
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy »

Yes, I find it in the basic_ios.h.

Code: Select all

/**
 *  @brief  The quick-and-easy status check.
 *
 *  This allows you to write constructs such as
 *  "if (!a_stream) ..." and "while (a_stream) ..."
*/
operator void*() const
{ return this->fail() ? 0 : const_cast<basic_ios*>(this); }
I stay home. Don't call me out.

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

Just to make clear, if (!a_stream) is using bool operator!, not operator void*.
For millions of years, mankind lived just like the animals. Then something happened which unleashed the power of our imagination. We learned to talk and we learned to listen...

saman_saadi
New poster
Posts: 31
Joined: Sun Feb 25, 2007 6:33 pm
Location: Tehran

Post by saman_saadi »

Krzysztof Duleba right there is a converstion operator. for example:

Code: Select all

#include <iostream>

using namespace std;

struct test
{
	int a;
};

int main()
{
	test a;
	while (a)	// Syntax error
	{
	}

	return 0;
}
but if you define a conversion operator the code will be correct:

Code: Select all

#include <iostream>

using namespace std;

struct test
{
	int a;
	operator bool()
	{
		return true;
	}
};

int main()
{
	test a;
	while (a)	// No syntax error
	{
	}

	return 0;
}
so you can use:
while (cin)
or
while (cin >> n)

for finding this operator see the source code of your compiler (I don't remember which header file). for example in MSDN there is a void * operator and there is no bool operator but if you see the source codes you will find a bool operator

Post Reply

Return to “C++”