[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Understanding the correct way to define "iostream" class?

Luna Moon

10/23/2008 12:47:00 AM

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
6 Answers

Ian Collins

10/23/2008 12:54:00 AM

0

Luna Moon wrote:
> 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.

--
Ian Collins

Luna Moon

10/23/2008 1:38:00 AM

0

On Oct 22, 5:54 pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Luna Moon wrote:
> > 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.
>
> --
> Ian Collins

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

Ian Collins

10/23/2008 2:39:00 AM

0

Luna Moon wrote:
> On Oct 22, 5:54 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> Luna Moon wrote:
>>> 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.
>>
*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;
}

--
Ian Collins

James Kanze

10/23/2008 8:55:00 AM

0

On Oct 23, 2:46 am, Luna Moon <lunamoonm...@gmail.com> wrote:
> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Luna Moon

10/23/2008 9:28:00 PM

0


> //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?

Ian Collins

10/23/2008 9:31:00 PM

0

Luna Moon wrote:
>> //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?

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

--
Ian Collins