Understanding the correct way to define "iostream" class?

L

Luna Moon

Here is a quote from the book: C++ Annotations,

I don't understand these sentences, could anybody help me?

thanks!

As a side effect to this implementation it must be stressed that it is
not anymore correct to declare
iostream objects using standard forward declarations, like:
class ostream; // now erroneous
Instead, sources that must declare iostream classes must
#include <iosfwd> // correct way to declare iostream classes
 
I

Ian Collins

Luna said:
Here is a quote from the book: C++ Annotations,

I don't understand these sentences, could anybody help me?

thanks!

As a side effect to this implementation it must be stressed that it is
not anymore correct to declare
iostream objects using standard forward declarations, like:
class ostream; // now erroneous
Instead, sources that must declare iostream classes must
#include <iosfwd> // correct way to declare iostream classes

iostream is a typedef for a template.
 
I

Ian Collins

Luna said:
*Plese* dont quote signatures.

Still don't understand? What does that lead to?

You can't forward declare something that's a typedef. If the compiler sees

class ostream;

then

typedef basic_ostream<char> ostream;

it will two different declarations of ostream.

Try a simple example:

struct Wibble;

struct X
{
Wibble* wibble;
};

template <typename T> struct Z {};

// Uncomment each of these and see what happens.
//
//typedef Z<int> Wibble;

//struct Wibble {};

int main()
{
X x;
}
 
J

James Kanze

Here is a quote from the book: C++ Annotations,

I'm curious: which book? (I ask because while fully
understandable, the text you quote was obviously not written by
a native speaker. And any decent publishing house would have
gotten it cleaned up.)
I don't understand these sentences, could anybody help me?
As a side effect to this implementation it must be stressed
that it is not anymore correct to declare iostream objects
using standard forward declarations, like:
class ostream; // now erroneous
Instead, sources that must declare iostream classes must
#include <iosfwd> // correct way to declare iostream classes

Well, it says what it says: traditionally, most header files
didn't include <iostream.h>; they just forwarded declared the
classes from it that they needed. Because in the standard,
istream and ostream are not classes, however, this technique is
no longer legal, and in modern C++, you have to include <iosfwd>
instead.
 
L

Luna Moon

//typedef Z<int> Wibble;

//struct Wibble {};

I tried. The first one didn't compile and the second one did compile
on my VC++ Express 2008.

What does that mean?
 
I

Ian Collins

Luna said:
I tried. The first one didn't compile and the second one did compile
on my VC++ Express 2008.

What does that mean?

Pay heed to the error the compiler gave you. I thought I'd explained
why you can't forward declare a typedef.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top