Multiple inclusion of the same header file can cause the compilation
to fail because of multiple definitions of the same type. That's why
it's standard practice to write all headers like this:
// SomeClass.hh
#ifndef SOME_CLASS_HH
#define SOME_CLASS_HH
class SomeClass
{
...
};
#endif
In complex projects recursive inclusion happens. It can be a rather
long chain of headers including other headers in a loop, and it may
be easy to miss.
The problem is that when this happens, the compiler errors are
usually very confusing. In the worst case (this has happened to me)
the compiler starts giving errors on places which have so far
compiled without problems and you haven't touched for weeks. It can
easily be really confusing why the compiler suddenly started to
complain about something you haven't even modified. Unless you have
experienced it before, it can be really hard to guess that the
problem is in a recursive inclusion which formed when you wrote
that one #include in a seemingly unrelated header. (Depending on
the compiler and the project settings it may be completely unexpected
what will "break" first because of that.)
I was thinking: In most cases when this problem has happened to me,
it was easy to fix by removing the #include and replacing it with a
pre-declaration of the class, as this usually has happened only when
I use references/pointers to that class instead of instances. Thus if
all headers were written like this:
// SomeClass.hh
class SomeClass;
#ifndef SOME_CLASS_HH
#define SOME_CLASS_HH
class SomeClass
{
...
};
#endif
then recursive inclusion would not be a problem. If you are only
using pointer/references, then everything will compile and work ok.
And even if you are using the class as a member variable and you
simply can't do that, you will most probably get a more meaningful
error message, like "error: field `x' has incomplete type" (now you
only have to understand that "incomplete type" refers to the type
having only been pre-declared).
Can anyone think of a drawback of doing this? 6 3856
On Jul 5, 1:30 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Multiple inclusion of the same header file can cause the compilation
to fail because of multiple definitions of the same type. That's why
it's standard practice to write all headers like this:
// SomeClass.hh
#ifndef SOME_CLASS_HH
#define SOME_CLASS_HH
class SomeClass
{
...
};
#endif
In complex projects recursive inclusion happens. It can be a rather
long chain of headers including other headers in a loop, and it may
be easy to miss.
The problem is that when this happens, the compiler errors are
usually very confusing. In the worst case (this has happened to me)
the compiler starts giving errors on places which have so far
compiled without problems and you haven't touched for weeks. It can
easily be really confusing why the compiler suddenly started to
complain about something you haven't even modified. Unless you have
experienced it before, it can be really hard to guess that the
problem is in a recursive inclusion which formed when you wrote
that one #include in a seemingly unrelated header. (Depending on
the compiler and the project settings it may be completely unexpected
what will "break" first because of that.)
I was thinking: In most cases when this problem has happened to me,
it was easy to fix by removing the #include and replacing it with a
pre-declaration of the class, as this usually has happened only when
I use references/pointers to that class instead of instances. Thus if
all headers were written like this:
// SomeClass.hh
class SomeClass;
#ifndef SOME_CLASS_HH
#define SOME_CLASS_HH
class SomeClass
{
...
};
#endif
then recursive inclusion would not be a problem. If you are only
using pointer/references, then everything will compile and work ok.
And even if you are using the class as a member variable and you
simply can't do that, you will most probably get a more meaningful
error message, like "error: field `x' has incomplete type" (now you
only have to understand that "incomplete type" refers to the type
having only been pre-declared).
Can anyone think of a drawback of doing this?
Ok, I can't see that you gain anything from this.
If you have double include guards, you have them.
If you don't have them, then adding this forward
declaration does not seem to gain you anything.
Socks
On Jul 5, 7:30 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Multiple inclusion of the same header file can cause the compilation
to fail because of multiple definitions of the same type. That's why
it's standard practice to write all headers like this:
// SomeClass.hh
#ifndef SOME_CLASS_HH
#define SOME_CLASS_HH
class SomeClass
{
...
};
#endif
In complex projects recursive inclusion happens. It can be a rather
long chain of headers including other headers in a loop, and it may
be easy to miss.
The problem is that when this happens, the compiler errors are
usually very confusing. In the worst case (this has happened to me)
the compiler starts giving errors on places which have so far
compiled without problems and you haven't touched for weeks. It can
easily be really confusing why the compiler suddenly started to
complain about something you haven't even modified. Unless you have
experienced it before, it can be really hard to guess that the
problem is in a recursive inclusion which formed when you wrote
that one #include in a seemingly unrelated header. (Depending on
the compiler and the project settings it may be completely unexpected
what will "break" first because of that.)
I was thinking: In most cases when this problem has happened to me,
it was easy to fix by removing the #include and replacing it with a
pre-declaration of the class, as this usually has happened only when
I use references/pointers to that class instead of instances. Thus if
all headers were written like this:
// SomeClass.hh
class SomeClass;
#ifndef SOME_CLASS_HH
#define SOME_CLASS_HH
class SomeClass
{
...
};
#endif
then recursive inclusion would not be a problem. If you are only
using pointer/references, then everything will compile and work ok.
And even if you are using the class as a member variable and you
simply can't do that, you will most probably get a more meaningful
error message, like "error: field `x' has incomplete type" (now you
only have to understand that "incomplete type" refers to the type
having only been pre-declared).
Can anyone think of a drawback of doing this?
Compile times will shoot up, since the typical optimization of
not re-opening a file protected by include guards won't work.
It masks, rather than corrects, a real problem.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Puppet_Sock wrote:
Ok, I can't see that you gain anything from this.
If what you are using is only references/pointers to the class,
the program will compile ok regardless of the recursive inclusion.
James Kanze wrote:
Compile times will shoot up, since the typical optimization of
not re-opening a file protected by include guards won't work.
You mean some compilers do that automatically? Which ones?
On Fri, 06 Jul 2007 12:48:56 +0300, Juha Nieminen wrote:
James Kanze wrote:
>Compile times will shoot up, since the typical optimization of not re-opening a file protected by include guards won't work.
You mean some compilers do that automatically? Which ones?
gcc definitely does it.
--
Markus Schoder
On Jul 7, 1:20 am, Markus Schoder <a3vr6dsg-use...@yahoo.dewrote:
On Fri, 06 Jul 2007 12:48:56 +0300, Juha Nieminen wrote:
James Kanze wrote:
Compile times will shoot up, since the typical optimization of not
re-opening a file protected by include guards won't work.
You mean some compilers do that automatically? Which ones?
gcc definitely does it.
gcc has been doing it for years. If others haven't followed
suite, they're asleep on the job. It's really not difficult.
--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JP SIngh |
last post by:
Thanks to Manohar for writing the basic code for displaying the managers and
the employees in a tree like structure.
I have adapted the code below but it gives me an error "exception occcured"...
|
by: Ali Asghar |
last post by:
Hi,
Please I need help.I have a problem of client side XSL
transformation. I sent the XML and the XSL to the client in XML data
islands. Using the transform Node method the HTML is returned....
|
by: Alexander Stippler |
last post by:
Hi,
I have a little problem to design some template classes in a realizable way.
I have some Container classes and some Proxy classes (proxies for
elements). Looks like this:
// P is the...
|
by: Kristian |
last post by:
This is my document body content:
<div id="nodeTree" class="nodeTree" title="nodeTree">
<div id="Hovedside" title="Hovedside" class="branch">
Hovedside
<div id="Produkter" title="Produkter"...
|
by: krema2ren |
last post by:
Hi
I've the following header problem that I need two classes to know each
other through a boost::shared_ptr. Does any of you smart guys have a
solution?
A.h
----------------------
#include...
|
by: Pindare |
last post by:
Hello,
I wrote quite a few c++ programs a some years ago which I compiled
successfully with g++ at the time, but now I forgot how to make them
work, so your help would be greatly appreciated (I...
|
by: Alkimake |
last post by:
here is the code that i am using for recursion to view the tree of my
database records.
recurs(48);
$i =0;
function recurs($ownerid) {
global $i;
$sqlOne="SELECT * FROM sub_cat WHERE...
|
by: RajinCodingForum |
last post by:
I have some idea but i am puzzled.
As i understand, file inclusion problems like x includes y and y in turn includes x etc. can be avoided by #ifdef preprocessor checks.
Can you please explain with...
|
by: xoinki |
last post by:
hi all,
I have 3 classes. class A, class B, and class C, class D, defined in 4 files A.cpp, B.cpp, C.cpp, D.cpp
inheritence hierarchy is as follows.
A--> B --> C
|
|
...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |