Connecting Tech Pros Worldwide Help | Site Map

why forward declarations

vishnu
Guest
 
Posts: n/a
#1: Nov 7 '05
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 .


Josh Mcfarlane
Guest
 
Posts: n/a
#2: Nov 7 '05

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?

John Harrison
Guest
 
Posts: n/a
#3: Nov 7 '05

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
Divick
Guest
 
Posts: n/a
#4: Nov 7 '05

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

Greg Comeau
Guest
 
Posts: n/a
#5: Nov 7 '05

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?
EventHelix.com
Guest
 
Posts: n/a
#6: Nov 8 '05

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

Roland Pibinger
Guest
 
Posts: n/a
#7: Nov 8 '05

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
Neelesh
Guest
 
Posts: n/a
#8: Nov 9 '05

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.

deane_gavin@hotmail.com
Guest
 
Posts: n/a
#9: Nov 9 '05

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

Neelesh
Guest
 
Posts: n/a
#10: Nov 9 '05

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