Connecting Tech Pros Worldwide Help | Site Map

why forward declarations

  #1  
Old November 7th, 2005, 06:45 AM
vishnu
Guest
 
Posts: n/a
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 .


  #2  
Old November 7th, 2005, 06:55 AM
Josh Mcfarlane
Guest
 
Posts: n/a

re: why forward declarations


vishnu wrote:[color=blue]
> 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 .[/color]

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?

  #3  
Old November 7th, 2005, 07:25 AM
John Harrison
Guest
 
Posts: n/a

re: why forward declarations


vishnu wrote:[color=blue]
> 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 .
>
>[/color]

Yes, lots of cases.

class X;

class Y
{
X x;
};

The above does not compile.

john
  #4  
Old November 7th, 2005, 07:45 AM
Divick
Guest
 
Posts: n/a

re: why forward declarations


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

  #5  
Old November 7th, 2005, 08:55 AM
Greg Comeau
Guest
 
Posts: n/a

re: why forward declarations


In article <1131348641.550981.285490@g49g2000cwa.googlegroups .com>,
Divick <divick.kishore@gmail.com> wrote:[color=blue]
>Slightly off topic. From my experience, forward declarations for
>classes and structs work but not for enums. Does any one have answers
>for that?[/color]

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?
  #6  
Old November 8th, 2005, 11:08 AM
EventHelix.com
Guest
 
Posts: n/a

re: why forward declarations


> what is the exact difference between including a class header file and[color=blue]
> forward declaration.
> and Is there a case , where in forward declaration is not possible and
> including is .[/color]

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

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

  #7  
Old November 8th, 2005, 08:25 PM
Roland Pibinger
Guest
 
Posts: n/a

re: why forward declarations


On 8 Nov 2005 02:52:04 -0800, "EventHelix.com" <eventhelix@gmail.com>
wrote:[color=blue]
>The following article describes how forward declarations can be used to
>reduce header file dependencies.
>
>http://www.eventhelix.com/RealtimeMa...dePatterns.htm[/color]

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
  #8  
Old November 9th, 2005, 08:35 AM
Neelesh
Guest
 
Posts: n/a

re: why forward declarations


Roland Pibinger wrote:[color=blue]
> 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);
> // ...
> };
>[/color]

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.

  #9  
Old November 9th, 2005, 11:45 AM
deane_gavin@hotmail.com
Guest
 
Posts: n/a

re: why forward declarations


Neelesh wrote:[color=blue]
> Roland Pibinger wrote:[color=green]
> > 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);
> > // ...
> > };
> >[/color]
>
> 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.[/color]

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

  #10  
Old November 9th, 2005, 11:55 AM
Neelesh
Guest
 
Posts: n/a

re: why forward declarations


deane_gavin@hotmail.com wrote:[color=blue]
> 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.[/color]

Yes, you are right. I was assuming that the definition was given along
with that declaration. Apologies.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templates: Code organization and forward declarations Rune Allnor answers 0 September 24th, 2008 09:35 PM
Forward declarations Carlos Martinez Garcia answers 2 February 16th, 2006 11:15 AM
Why does C++ require forward declarations? aleko answers 11 July 23rd, 2005 03:55 AM
forward declarations and namespaces? Steven T. Hatton answers 6 July 22nd, 2005 11:37 AM
Some problems with forward declarations mjm answers 3 July 19th, 2005 05:20 PM