473,379 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 .
Nov 7 '05 #1
9 1982
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
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
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
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
> 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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
10
by: Alan Lee | last post by:
Hi i am writing a small simulation for a bunch of atoms jumping around on a surface. I know i am a crappy programmer since i am not very familiar with object oriented languages. I am woindering...
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
11
by: aleko | last post by:
This applies equally to function prototypes. Why do we have to tell the compiler twice? Other modern compiled languages don't have this requirement. Just curious, Aleko
12
by: fox | last post by:
How do I (portably) make a forward reference to a static (I mean file-scope) variable? I've been using "extern" for years, for example: extern int x; int foo(void) { return x++; }
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
2
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: Rune Allnor | last post by:
Hi all. I have these classes, implemented as templates in header files. Right now I have one file per class, where both the declarations and implementations of one class are located in each...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.