Understanding the correct way to define "iostream" class?

  • Follow


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
0
Reply lunamoonmoon (258) 10/23/2008 12:46:36 AM

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
0
Reply ian-news (9886) 10/23/2008 12:54:21 AM


On Oct 22, 5:54=A0pm, 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?
0
Reply lunamoonmoon (258) 10/23/2008 1:37:58 AM

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
0
Reply ian-news (9886) 10/23/2008 2:39:23 AM

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=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
0
Reply james.kanze (9594) 10/23/2008 8:54:37 AM

> //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?
0
Reply lunamoonmoon (258) 10/23/2008 9:28:01 PM

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
0
Reply ian-news (9886) 10/23/2008 9:31:17 PM

6 Replies
32 Views

(page loaded in 1.275 seconds)


Reply: