473,407 Members | 2,326 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,407 software developers and data experts.

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

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

Sep 3 '07 #1
6 1441
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
Sep 3 '07 #2
On 2007-09-03 18:06, 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?
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.

--
Erik Wikström
Sep 3 '07 #3

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;

Sep 3 '07 #4
On 2007-09-03 23:47, Jorgen Grahn wrote:
On Mon, 03 Sep 2007 09:06:48 -0700, Goran <po**********@gmail.comwrote:
>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?
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.

--
Erik Wikström
Sep 3 '07 #5
On 2007-09-03 18:24:12 -0400, Erik Wikström <Er***********@telia.comsaid:
On 2007-09-03 23:47, Jorgen Grahn wrote:
>>
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.
Hypothetically, maybe. But the standard is explicit: if you use the
name without including the standard header, the behavior is undefined.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 3 '07 #6
On Sep 3, 7:05 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-03 18:06, Goran wrote:
[...]
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.
No. As others have pointed out, std::string is not a class, so
the forward declaration doesn't work.

--
James Kanze (GABI Software) email:james.ka...@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

Sep 4 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
2
by: steve | last post by:
Hi, I need to do conditional script "include", but like to pull the code from db instead of a file. How do I do that? Reason: I like to implement some complex regex logic, and make it table...
4
by: Simon Wigzell | last post by:
I often use : <!--#include file="filename"--> in my code to include often used bits of asp code. I told someone in another newsgroup about it but now I'm wondering if it will be supported for...
7
by: mescaline | last post by:
Hi, Suppose a_file.cpp contains a function a_function() Now to include it in main_file.cpp I just do #include "a_file.cpp" and I'm all set. i recently came across this seemingly roundabout...
18
by: Tuckers | last post by:
My question is, if I have created my own library which lives in its own install directory, to refer to its header file is it better to use #include "MyLibrary.h" or #include <MyLibrary.h> ...
4
by: bibsoconner | last post by:
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all...
2
by: untitled | last post by:
i included the following line to VC++ after i installed the directx SDK and platform SDK. #include "DShow.h" i configured c directories for the include and lib to both directx SDX and platform...
5
by: Summercoolness | last post by:
if you are just a users among the 100s of users on a hosting machines, then if you do a include("/main.php"); it won't include the file but will say Failed opening required '/main.php'...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.