Question in terms of design. Where to put "#include"?

G

Goran

Hi all,

this is just a "design" question.

I'm unsure where to put my "#includes".

Should I put it directly into headerfiles? Or into sourcefiles with a
forward type declaration in headerfiles?

And if forward type declarations in headerfiles are used... why can't
I do something like...

class string;

and include string in the source file?

Thanks

Goran
 
B

Bo Persson

Goran wrote:
:: Hi all,
::
:: this is just a "design" question.
::
:: I'm unsure where to put my "#includes".
::
:: Should I put it directly into headerfiles? Or into sourcefiles
:: with a forward type declaration in headerfiles?

It depends on how much of a class you really use in the header.

To form a pointer or a reference, a declarations is enough. If you
call a member function, for example, you need the full definition.

::
:: And if forward type declarations in headerfiles are used... why
:: can't I do something like...
::
:: class string;
::
:: and include string in the source file?

Becase string isn't really a class, but a typedef for a template.


Bo Persson
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi all,

this is just a "design" question.

I'm unsure where to put my "#includes".

Should I put it directly into headerfiles? Or into sourcefiles with a
forward type declaration in headerfiles?

Using forward declarations if you can is a good idea.
And if forward type declarations in headerfiles are used... why can't
I do something like...

class string;

and include string in the source file?

You can, sometimes. It depends on how you are using strings in your
header, for some things the compiler need to know more about the type
and then a forward declaration is not enough. As long as you only use
references or pointers to string then you should be fine.
 
B

BobR

Goran said:
Hi all,

this is just a "design" question.
I'm unsure where to put my "#includes".

Should I put it directly into headerfiles? Or into sourcefiles with a
forward type declaration in headerfiles?

Put them in the .h and the .cpp. What if you later remove the header from
the .cpp file. Something later in the chain might depend on the headers
removed (in the .h).
And if forward type declarations in headerfiles are used... why can't
I do something like...

class string;

and include string in the source file?
Thanks, Goran

Put the headers everywhere they are needed. The inclusion guards prevent
them being compiled a second time in a translation unit, and it's a form of
documentation ( #include <string> == "Hey, I'm using std::string in this
file!").


A *weird* example I threw together to demonstrate how includes work:

// --- Guts.h ---
std::cout<<"Hello World, from Guts.h"<<std::endl;
#define BLAHBLAH cout<<"Hello World, from Guts.h"<<std::endl;
// be sure that is one single line!
// --- Guts.h --- END

// - Main.cpp -
#include <iostream>
int main(){
#include "Guts.h" // yep, right here
BLAHBLAH
// note: no semicolon after that line.
return 0;
}
// - Main.cpp -END

The "Guts.h" totally depends on the "#include <iostream>" being exactly
where it is in this case (not in the Guts.h file).
This just shows that the compiler simply substitutes the contents of the .h
file where it is included.
You seldom, *if ever*, go to that extreme. <G>
 
B

BobR

BobR wrote in message...
// --- Guts.h ---
std::cout<<"Hello World, from Guts.h"<<std::endl;
#define BLAHBLAH cout<<"Hello World, from Guts.h"<<std::endl;

Oops, that should be:
#define BLAHBLAH std::cout<<"Hello World, from Guts.h"<<std::endl;
 
J

Jorgen Grahn

Hi all,

this is just a "design" question.

I'm unsure where to put my "#includes".

Should I put it directly into headerfiles? Or into sourcefiles with a
forward type declaration in headerfiles?

One common guideline (which some people disagree with) is that if you
have a header file foo.h, a file which just contains

#include "foo.h"

should compile. I.e., foo.h should include everything it needs to
compile (but not too much more).
And if forward type declarations in headerfiles are used... why can't
I do something like...

class string;

and include string in the source file?

You /can/ do that, much of the time. There are rules for when you can
and when you cannot. If you're talking about std::string, it won't
work, because ... well, I guess std::string being a typedef is one
reason?

/Jorgen
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

One common guideline (which some people disagree with) is that if you
have a header file foo.h, a file which just contains

#include "foo.h"

should compile. I.e., foo.h should include everything it needs to
compile (but not too much more).


You /can/ do that, much of the time. There are rules for when you can
and when you cannot. If you're talking about std::string, it won't
work, because ... well, I guess std::string being a typedef is one
reason?

It should be possible, the problem is that it is a parametrised type, so
you have to include all the template parameters in the forward
declaration, which would require a forward declaration of char_traits
too if I am not mistaken.
 
P

Pete Becker

It should be possible, the problem is that it is a parametrised type,
so you have to include all the template parameters in the forward
declaration, which would require a forward declaration of char_traits
too if I am not mistaken.

Hypothetically, maybe. But the standard is explicit: if you use the
name without including the standard header, the behavior is undefined.
 
J

James Kanze

On 2007-09-03 18:06, Goran wrote:

[...]
You can, sometimes. It depends on how you are using strings in your
header, for some things the compiler need to know more about the type
and then a forward declaration is not enough. As long as you only use
references or pointers to string then you should be fine.

No. As others have pointed out, std::string is not a class, so
the forward declaration doesn't work.
 
J

Jim Langston

Goran said:
Hi all,

this is just a "design" question.

I'm unsure where to put my "#includes".

Should I put it directly into headerfiles? Or into sourcefiles with a
forward type declaration in headerfiles?

And if forward type declarations in headerfiles are used... why can't
I do something like...

class string;

and include string in the source file?

Put the #include for any file that needs it, .h or .cpp. If you don't you
get into the extremely irritating situation of order of includes. If an .h
file needs to use the std::string class, #include <string> inside the
header. If it was already included in some other header or the .cpp file no
harm no foul. the include guards in <string> will make sure it's not
included again. You can't do any harm by including an header file that's
already been included somewhere else (with proper usage of include guards)
but if you do not include a needed file it can be a real pain to maintain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top