Connecting Tech Pros Worldwide Help | Site Map

why forward declarations

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 7th, 2005, 05:45 AM
vishnu
Guest
 
Posts: n/a
Default why forward declarations

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, 05:55 AM
Josh Mcfarlane
Guest
 
Posts: n/a
Default 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, 06:25 AM
John Harrison
Guest
 
Posts: n/a
Default 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, 06:45 AM
Divick
Guest
 
Posts: n/a
Default 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, 07:55 AM
Greg Comeau
Guest
 
Posts: n/a
Default 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, 10:08 AM
EventHelix.com
Guest
 
Posts: n/a
Default 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, 07:25 PM
Roland Pibinger
Guest
 
Posts: n/a
Default 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, 07:35 AM
Neelesh
Guest
 
Posts: n/a
Default 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, 10:45 AM
deane_gavin@hotmail.com
Guest
 
Posts: n/a
Default 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, 10:55 AM
Neelesh
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.