<fstream.h> , no need for <iostream.h>?

C

Charles L

I have read that the inclusion of <fstream.h> makes the inclusion of
<iostream.h> unnecessary.

Is this correct?

Charles L
 
U

Unforgiven

Charles L said:
I have read that the inclusion of <fstream.h> makes the inclusion of
<iostream.h> unnecessary.

Is this correct?

First of all, don't use <fstream.h> and <iostream.h>. Use <fstream> and
<iostream> instead.

As for your question: I don't know if that behaviour is standard defined,
but on my system at least (VC7.1) including <fstream> does not make
inclusion of <iostream> unnecessary. So the answer is no.
 
V

Victor Bazarov

Unforgiven said:
First of all, don't use <fstream.h> and <iostream.h>. Use <fstream> and
<iostream> instead.

As for your question: I don't know if that behaviour is standard
defined, but on my system at least (VC7.1) including <fstream> does not
make inclusion of <iostream> unnecessary. So the answer is no.

The Standard mandates that different pieces are declared in different
headers. To use a certain element (class, template, object) one need
to include the corresponding header. It should be a simple rule to
follow, actually. You need 'cout' or 'cerr'? Use <iostream>. You
need 'string'? Use <string>. You need 'sort', use <algorithm>. Etc.

One should never concern oneself with (whether platform- or compiler-
dependent) cross-references between standard headers, even if there is
a logical connection between some of them which might suggest such
cross-reference (or inclusion of one into the other).

Victor
 
K

Keith Meidling

First of all, don't use <fstream.h> and <iostream.h>. Use <fstream> and
<iostream> instead.

As for your question: I don't know if that behaviour is standard defined,
but on my system at least (VC7.1) including <fstream> does not make
inclusion of <iostream> unnecessary. So the answer is no.
I'm taking a class in C++ and they use <iostream> rather than
<iostream.h> in the examples.

What is the reason for using one over the other. I know that if
you use the .h you don't have to use 'using std::xxxxxx' statements.
 
D

David Harmon

On Tue, 29 Jun 2004 14:15:29 -0500 in comp.lang.c++, Keith Meidling
I'm taking a class in C++ and they use <iostream> rather than
<iostream.h> in the examples.

<isotream> is part of ISO standard C++. <iostream.h> is not and never
has been. The only reason to use <iostream.h> any longer is in support
of ancient pre-standard code, and that will be painful with many
puzzling incompatibilities with the rest of the standard library. This
situation is AFAIK unique among the typical vendor's headers.

Probably best to go into your include directory and rename iostream.h
to legacy_iostream_do_not_use.h.

The other situation is headers inherited from C. In that case you can
probably choose <xxxxx.h> or <cxxxxx> without insurmountable problems.

Other standard C++ headers have no .h at all.
 
R

Richard Herring

Keith Meidling said:
I'm taking a class in C++ and they use <iostream> rather than
<iostream.h> in the examples.

What is the reason for using one over the other. I know that if
you use the .h you don't have to use 'using std::xxxxxx' statements.
You make it sound like a penance ;-)
Having to use 'using std::xxx' (or prefix names with std::)
is a _good_ thing. Typing those few extra characters now may save you a
lot of work later.
 
K

Keith Meidling

You make it sound like a penance ;-)
Having to use 'using std::xxx' (or prefix names with std::)
is a _good_ thing. Typing those few extra characters now may save you a
lot of work later.

How would this save me work?

I don't want to sound like I'm questioning your statement, I'd just
like more info... If it's better to do it this way, hey, I'm all for it
I'd just like to know why to use one or the other...
 
R

Richard Herring

Keith Meidling said:
How would this save me work?

I don't want to sound like I'm questioning your statement, I'd just
like more info... If it's better to do it this way, hey, I'm all for it
I'd just like to know why to use one or the other...

The technical term is "namespace pollution". The <*.h> headers dump
their contents into the global namespace; the ones without .h define
them in namespace std. With a big enough project, sooner or later
you're going to give one of your functions the same name as a standard
library function, or you'll use a third-party library which does so.
Using namespaces helps to prevent ambiguities.

More subtle, but sometimes more important, is the way the compiler looks
up functions depending on the type of their arguments and the namespace
in which they are defined.
 
P

Prateek R Karandikar

Charles L said:
I have read that the inclusion of <fstream.h> makes the inclusion of
<iostream.h> unnecessary.

Is this correct?

Not at all. There exist no such Standard Headers. Here is a complete
list of Standard Headers, of which the last 18 are deprecated:

<algorithm> <iomanip> <list> <queue> <streambuf> <bitset> <ios>
<locale> <set> <string> <complex> <iosfwd> <map> <sstream> <typeinfo>
<deque> <iostream> <memory> <stack> <utility> <exception> <istream>
<new> <stdexcept> <valarray> <fstream> <iterator> <numeric>
<strstream> <vector> <cassert> <ciso646> <csetjmp> <cstdio> <ctime>
<cctype> <climits> <csignal> <cstdlib> <cwchar> <cerrno> <clocale>
<cstdarg> <cstring> <cwctype> <assert.h> <iso646.h> <setjmp.h>
<stdio.h> <wchar.h> <ctype.h> <limits.h> <signal.h> <stdlib.h>
<wctype.h> <errno.h> <locale.h> <stdarg.h> <string.h>

-- --
Abstraction is selective ignorance.
-Andrew Koenig
-- --
 
P

Prateek R Karandikar

I'm taking a class in C++ and they use said:
<iostream.h> in the examples.

What is the reason for using one over the other. I know that if
you use the .h you don't have to use 'using std::xxxxxx' statements.

<iostream> exists. <iostream.h> doesn't exist.

-- --
Abstraction is selective ignorance.
-Andrew Koenig
-- --
 
K

Keith Meidling

What is the reason for using one over the other. I know that if
The technical term is "namespace pollution". The <*.h> headers dump
their contents into the global namespace; the ones without .h define
them in namespace std. With a big enough project, sooner or later
you're going to give one of your functions the same name as a standard
library function, or you'll use a third-party library which does so.
Using namespaces helps to prevent ambiguities.

More subtle, but sometimes more important, is the way the compiler looks
up functions depending on the type of their arguments and the namespace
in which they are defined.

Thanks for the information...
I'll start changing my code to reflect the std namespace.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top