ofstream is_open

S

Steven C

ofstream blah;

IS:

if (blah == NULL)
{
}

THE SAME AS:

if (!blah.is_open())
{
}

I think the answer is NO since blah will never equal NULL, since I
could not find a operator for ofstream for == and blah is a instance
of a class and not a reference or a pointer therefore the statement
blah == NULL makes no sense.
 
R

Ron Natalie

I think the answer is NO since blah will never equal NULL, since I
could not find a operator for ofstream for == and blah is a instance
of a class and not a reference or a pointer therefore the statement
blah == NULL makes no sense.

The base class for the streams overload both a conversion operator for void*
and also operator!. Hence you can say things like:
if(blah)
or
if(!blah).

These are mapped to the blah.fail() (which emcompasses more errors than not
being
open).
 
S

Sam Holden

ofstream blah;

IS:

if (blah == NULL)
{
}

THE SAME AS:

if (!blah.is_open())
{
}

I think the answer is NO since blah will never equal NULL, since I
could not find a operator for ofstream for == and blah is a instance
of a class and not a reference or a pointer therefore the statement
blah == NULL makes no sense.

ofstream has a conversion operator to void*, so comparing it with NULL
is perfectly valid.

However, they have completely different meanings, the first checks if
failbit or badbit are true, the second tests if blah.is_open() returns
false. Those are completely different tests.

Hence the answer is no, but for a different reason.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top