By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

why forward declarations

P: 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 .
Nov 7 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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?

Nov 7 '05 #2

P: n/a
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
Nov 7 '05 #3

P: n/a
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

Nov 7 '05 #4

P: n/a
In article <11**********************@g49g2000cwa.googlegroups .com>,
Divick <di************@gmail.com> 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?
Nov 7 '05 #5

P: 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 .


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

Nov 8 '05 #6

P: n/a
On 8 Nov 2005 02:52:04 -0800, "EventHelix.com" <ev********@gmail.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
Nov 8 '05 #7

P: n/a
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.

Nov 9 '05 #8

P: n/a
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 '05 #9

P: n/a
de*********@hotmail.com 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.

Nov 9 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.