Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why forward declarations

Reply
Thread Tools

why forward declarations

 
 
vishnu
Guest
Posts: n/a
 
      11-07-2005
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 .


 
Reply With Quote
 
 
 
 
Josh Mcfarlane
Guest
Posts: n/a
 
      11-07-2005
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?

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-07-2005
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
 
Reply With Quote
 
Divick
Guest
Posts: n/a
 
      11-07-2005
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

 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      11-07-2005
In article < .com>,
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?
 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      11-08-2005
> 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/RealtimeMa...dePatterns.htm

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

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      11-08-2005
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/RealtimeMa...dePatterns.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
 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005
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.

 
Reply With Quote
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      11-09-2005
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

 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why does C++ require forward declarations? aleko C++ 11 04-04-2005 10:00 AM
Forward declarations and namespaces whithers C++ 4 01-16-2004 07:12 AM
Some problems with forward declarations mjm C++ 3 08-13-2003 12:48 AM
namespaces and forward declarations matthew polder C++ 1 07-24-2003 04:06 PM



Advertisments