why forward declarations

Discussion in 'C++' started by vishnu, Nov 7, 2005.

  1. vishnu

    vishnu Guest

    what is the exact difference between including a class header file and
    forward declaration.
    and Is there a case , where in forward declaration is not possible and
    including is .
     
    vishnu, Nov 7, 2005
    #1
    1. Advertising

  2. vishnu wrote:
    > what is the exact difference between including a class header file and
    > forward declaration.
    > and Is there a case , where in forward declaration is not possible and
    > including is .


    Seems like a homework question. But, think about it. Forward
    declaration simply tells the compiler that class foo is a class.
    Including it tells the compiler a lot more (hint hint). Now what case
    would a forward declaration be possible but including would work?
     
    Josh Mcfarlane, Nov 7, 2005
    #2
    1. Advertising

  3. vishnu wrote:
    > what is the exact difference between including a class header file and
    > forward declaration.
    > and Is there a case , where in forward declaration is not possible and
    > including is .
    >
    >


    Yes, lots of cases.

    class X;

    class Y
    {
    X x;
    };

    The above does not compile.

    john
     
    John Harrison, Nov 7, 2005
    #3
  4. vishnu

    Divick Guest

    Slightly off topic. From my experience, forward declarations for
    classes and structs work but not for enums. Does any one have answers
    for that?

    Divick
     
    Divick, Nov 7, 2005
    #4
  5. vishnu

    Greg Comeau Guest

    In article <>,
    Divick <> wrote:
    >Slightly off topic. From my experience, forward declarations for
    >classes and structs work but not for enums. Does any one have answers
    >for that?


    That's right, C++ does not allow it, under the premise
    that enum's are allowed to be different sizes, and by the
    time it's decided what that size should be might be too late.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Nov 7, 2005
    #5
  6. > what is the exact difference between including a class header file and
    > forward declaration.
    > and Is there a case , where in forward declaration is not possible and
    > including is .


    The following article describes how forward declarations can be used to
    reduce header file dependencies.

    http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm

    --
    EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
    Sequence Diagram Based System Design and Object Modeling Tool
     
    EventHelix.com, Nov 8, 2005
    #6
  7. On 8 Nov 2005 02:52:04 -0800, "EventHelix.com" <>
    wrote:
    >The following article describes how forward declarations can be used to
    >reduce header file dependencies.
    >
    >http://www.eventhelix.com/RealtimeMantra/HeaderFileIncludePatterns.htm


    Good informative article! You can forward declare even more than you
    describe, e.g. arguments passed by value and and returned by value:

    class E;
    class F;

    class A {
    public:
    E foo (F f);
    // ...
    };

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Nov 8, 2005
    #7
  8. vishnu

    Neelesh Guest

    Roland Pibinger wrote:
    > You can forward declare even more than you
    > describe, e.g. arguments passed by value and and returned by value:
    >
    > class E;
    > class F;
    >
    > class A {
    > public:
    > E foo (F f);
    > // ...
    > };
    >


    That won't work. The definitions of E and F are essential for this code
    to compile - a forward declaration of a class is sufficient only if we
    are not inplicitly or explicitly requesting for size of the class or
    are referring to any members.
     
    Neelesh, Nov 9, 2005
    #8
  9. vishnu

    Guest

    Neelesh wrote:
    > Roland Pibinger wrote:
    > > You can forward declare even more than you
    > > describe, e.g. arguments passed by value and and returned by value:
    > >
    > > class E;
    > > class F;
    > >
    > > class A {
    > > public:
    > > E foo (F f);
    > > // ...
    > > };
    > >

    >
    > That won't work. The definitions of E and F are essential for this code
    > to compile - a forward declaration of a class is sufficient only if we
    > are not inplicitly or explicitly requesting for size of the class or
    > are referring to any members.


    Comeau online compiles it. It complains about using an incomplete type
    if I add a member of type E to the class though:

    E e;

    added after the declaration of the foo function.

    I don't have the standard to hand to check (I am relying on the fact
    that Comeau doesn't get much wrong) but it would seem to me that the
    definitions of E and F are not needed (e.g. to know the sizes of
    objects of those types) until the _definition_ of the foo member
    function.

    Gavin Deane
     
    , Nov 9, 2005
    #9
  10. vishnu

    Neelesh Guest

    wrote:
    > I don't have the standard to hand to check (I am relying on the fact
    > that Comeau doesn't get much wrong) but it would seem to me that the
    > definitions of E and F are not needed (e.g. to know the sizes of
    > objects of those types) until the _definition_ of the foo member
    > function.


    Yes, you are right. I was assuming that the definition was given along
    with that declaration. Apologies.
     
    Neelesh, Nov 9, 2005
    #10
    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. matthew polder

    namespaces and forward declarations

    matthew polder, Jul 24, 2003, in forum: C++
    Replies:
    1
    Views:
    367
    John Harrison
    Jul 24, 2003
  2. mjm
    Replies:
    3
    Views:
    409
  3. whithers
    Replies:
    4
    Views:
    386
    Sumit Rajan
    Jan 16, 2004
  4. aleko
    Replies:
    11
    Views:
    4,120
    Rolf Magnus
    Apr 4, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,218
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page