 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
michaelkatsilis@yahoo.com Guest
|
Posted: Mon Apr 18, 2005 9:09 am Post subject: when to use forward declarations and what are the main issue |
|
|
As far as I can understand, the use of forward declarations is to
reduce compilation times and dependancies between header files.
Are there other reasons?
Also, what are the main issues surrounding the use of forward
declarations?
Regards,
Michael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Timo Geusch Guest
|
Posted: Tue Apr 19, 2005 10:23 pm Post subject: Re: when to use forward declarations and what are the main |
|
|
[email]michaelkatsilis (AT) yahoo (DOT) com[/email] wrote:
| Quote: | As far as I can understand, the use of forward declarations is to
reduce compilation times and dependancies between header files.
Are there other reasons?
|
Not that I can think of at the moment.
| Quote: | Also, what are the main issues surrounding the use of forward
declarations?
|
Main problem is that they'll only work in those cases where the
compiler doesn't have to see the whole class to figure out its size. In
other words, you can only use forward declarations if the source code
you use them in references objects of the forward declared class via
pointers or references.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Tue Apr 19, 2005 10:54 pm Post subject: Re: when to use forward declarations and what are the main i |
|
|
[email]michaelkatsilis (AT) yahoo (DOT) com[/email] wrote:
| Quote: | As far as I can understand, the use of forward declarations is to
reduce compilation times and dependancies between header files.
|
Maybe a bit simplified.
The reason to use forward declarations is, you need to use a
symbol (generally a class name) that hasn't been declared yet.
Assuming that the header files have been created in a sane way,
you can usually do the same thing by #including the appropriate
header files. One of the many reasons to use forward
declarations instead is to reduce compilation times, because
when you only need to use the name and don't need the entire
definition then the forward declares are generally faster.
| Quote: | Are there other reasons?
|
Yes.
When two classes are co-dependant, you need to use forward
declarations so that whichever one is included first can use the
name of the other one.
If you specifically do NOT want to allow clients to see your
private classes, you can use a forward declaration instead. (See
for example the "pimpl" pattern.)
| Quote: | Also, what are the main issues surrounding the use of forward
declarations?
|
Yes.
There are things you cannot do with forward declarations that you
can do with the full definitions. Some of these do not require a
compiler diagnostic. For instance, if you try to create an
auto_ptr<> of a class that hasn't been defined, you will get
undefined behavior, possibly including not calling destructors.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
spip_yeah@yahoo.com Guest
|
Posted: Tue Apr 19, 2005 10:56 pm Post subject: Re: when to use forward declarations and what are the main i |
|
|
I would also add that it allows you not to reveal any types that a
client of your class doesn't need to know. But if your class needs that
type internally, it most likely needs that type as a class attribute
which reveals it to clients of your class. A forward declaration will
take care of that as long as you have a pointer to the type as opposed
to an instance of the type.
[email]michaelkatsilis (AT) yahoo (DOT) com[/email] wrote:
| Quote: | As far as I can understand, the use of forward declarations is to
reduce compilation times and dependancies between header files.
Are there other reasons?
Also, what are the main issues surrounding the use of forward
declarations?
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed Apr 20, 2005 9:33 am Post subject: Re: when to use forward declarations and what are the main i |
|
|
[email]michaelkatsilis (AT) yahoo (DOT) com[/email] wrote:
| Quote: | As far as I can understand, the use of forward declarations is
to reduce compilation times and dependancies between header
files.
Are there other reasons?
|
It permits cyclic dependencies -- class A uses class B, and vice
versa.
| Quote: | Also, what are the main issues surrounding the use of forward
declarations?
|
The day someone converts the class into a template and a
typedef, you're screwed. (Remember all the headers with "class
ostream;" and "class istream;".)
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Wed Apr 20, 2005 4:53 pm Post subject: Re: when to use forward declarations and what are the main |
|
|
[email]tnews (AT) unix-consult (DOT) com[/email] (Timo Geusch) wrote (abridged):
| Quote: | Also, what are the main issues surrounding the use of forward
declarations?
Main problem is that they'll only work in those cases where the
compiler doesn't have to see the whole class to figure out its size. In
other words, you can only use forward declarations if the source code
you use them in references objects of the forward declared class via
pointers or references.
|
Depending on what you mean by "source code". Function declarations are OK.
Eg:
class MyClass;
MyClass func( MyClass c );
Func passes and returns by value, without pointers or references, but its
declaration is allowed. This is useful when declaration and definition
(which does need a complete type) are in different files, eg .hpp and
..cpp.
-- Dave Harris, Nottingham, UK.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Timo Geusch Guest
|
Posted: Thu Apr 21, 2005 9:07 am Post subject: Re: when to use forward declarations and what are the main i |
|
|
Dave Harris wrote:
| Quote: | tnews (AT) unix-consult (DOT) com (Timo Geusch) wrote (abridged):
Also, what are the main issues surrounding the use of forward
declarations?
Main problem is that they'll only work in those cases where the
compiler doesn't have to see the whole class to figure out its
size. In other words, you can only use forward declarations if the
source code you use them in references objects of the forward
declared class via pointers or references.
Depending on what you mean by "source code". Function declarations
are OK. Eg:
class MyClass;
MyClass func( MyClass c );
Func passes and returns by value, without pointers or references, but
its declaration is allowed. This is useful when declaration and
definition (which does need a complete type) are in different files,
eg .hpp and .cpp.
|
Fair enough, I didn't think of function declarations in a header file.
However, if you're using 'MyClass' to declare a data member of a class
in a header file, you're "stuck" with:
MyClass * member_;
or
MyClass & member_;
whereas
MyClass member_;
would require you to include the declaration of MyClass instead of just
providing a forward declaration of MyClass.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|