Understanding the correct way to define "iostream" class?

Discussion in 'C++' started by Luna Moon, Oct 23, 2008.

  1. Luna Moon

    Luna Moon Guest

    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
     
    Luna Moon, Oct 23, 2008
    #1
    1. Advertising

  2. Luna Moon

    Ian Collins Guest

    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
     
    Ian Collins, Oct 23, 2008
    #2
    1. Advertising

  3. Luna Moon

    Luna Moon Guest

    On Oct 22, 5:54 pm, Ian Collins <> 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?
     
    Luna Moon, Oct 23, 2008
    #3
  4. Luna Moon

    Ian Collins Guest

    Luna Moon wrote:
    > On Oct 22, 5:54 pm, Ian Collins <> 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
     
    Ian Collins, Oct 23, 2008
    #4
  5. Luna Moon

    James Kanze Guest

    On Oct 23, 2:46 am, Luna Moon <> 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:
    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
     
    James Kanze, Oct 23, 2008
    #5
  6. Luna Moon

    Luna Moon Guest


    > //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?
     
    Luna Moon, Oct 23, 2008
    #6
  7. Luna Moon

    Ian Collins Guest

    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
     
    Ian Collins, Oct 23, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Tiger
    Replies:
    10
    Views:
    5,599
  2. ai@work
    Replies:
    9
    Views:
    546
    Ron Natalie
    Dec 16, 2004
  3. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    777
    red floyd
    Jan 15, 2005
  4. red floyd
    Replies:
    3
    Views:
    536
    Dietmar Kuehl
    Mar 8, 2005
  5. Peng Yu
    Replies:
    3
    Views:
    515
Loading...

Share This Page