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

Proper Including Style

Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires string. Is it
good form to repeat the <string> include to make the dependency
explicit, or do just allow the include to be make implicitly through
the .h include? That is, should the header of your .cpp file be:

#include "CustomClass.h"

OR:

#include <string>
#include "CustomClass.h"

thanks for any thoughts,
cpp
Jul 22 '05 #1
11 1910
Just include it in your header file. There's no sense in including it
twice.

-Howard

"All programmers write perfect code.
....All other programmers write crap."

"I'm never wrong.
I thought I was wrong once,
but I was mistaken."
Jul 22 '05 #2
On Thu, 03 Jun 2004 19:26:26 GMT, cppaddict <he***@hello.com> wrote:
Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires string. Is it
good form to repeat the <string> include to make the dependency
explicit, or do just allow the include to be make implicitly through
the .h include? That is, should the header of your .cpp file be:

#include "CustomClass.h"

OR:

#include <string>
#include "CustomClass.h"

thanks for any thoughts,
cpp


I currently favor putting the appropriate #includes in each source file
that uses them. The cost (in terms of increased compilation time due to
possible loss of precompiled header caching, if any) should be minimal on
modern computers. Anyone can look at the source file out of context and see
the facilities it directly uses (if there's an agreement on that style).

No one who sees those facilities being used would have to wonder whether
their headers are being properly included. This is a serious issue,
because some platforms' standard headers end up including other standard
headers (or the moral equivalent thereof), and others don't. A file might
compile fine on one platform even if a specific standard header is /never/
included, and this would cause the code to break when recompiled on a
platform that doesn't allow you to get away with that. This isn't the case
you've outlined above, specifically, but someone reading the code using
your first approach (everything included in the custom header, but not in
the file that includes it) wouldn't know that for sure unless they tracked
down all the headers.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3
Leor Zolman wrote:

I currently favor putting the appropriate #includes in each source file
that uses them. The cost (in terms of increased compilation time due to
possible loss of precompiled header caching, if any) should be minimal on
modern computers. Anyone can look at the source file out of context and see
the facilities it directly uses (if there's an agreement on that style).


I agree. It's better to be clear than to be clever.

--
Mike Smith

Jul 22 '05 #4
cppaddict wrote:
Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires
string. Is it good form to repeat the <string> include
to make the dependency explicit, or do just allow the
include to be make implicitly through the .h include?
That is, should the header of your .cpp file be:

#include "CustomClass.h"

OR:

#include <string>
#include "CustomClass.h"

thanks for any thoughts,
cpp


I generally put all the includes in the header files, and have the .cpp
files only include their corresponding .h files. Part of the reason is that
duplication is bad (if you have to add a header file, you have to add it to
two separate places).
Jul 22 '05 #5
cppaddict wrote:
Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires string. Is it
good form to repeat the <string> include to make the dependency
explicit, or do just allow the include to be make implicitly through
the .h include? That is, should the header of your .cpp file be:

#include "CustomClass.h"

OR:

#include <string>
#include "CustomClass.h"


The set of includes (which is usually located at the beginning of
the file) should contain _all_ necessary headers/files that the
current file needs.

Example (include guards in headers and some functionality are
omitted for simplicity):

----------------------------- A.h
class A {
// blah
};
----------------------------- B.h
#include "A.h"
class B {
A a;
void foo();
};
----------------------------- B.cpp
#include "B.h"
#include "A.h"
#include "somethingelse.h"

void B::foo() {
do_somethingelse(a);
}
----------------------------------

Now, that's what I'd do in B.cpp. Why? Imagine that somebody
relies on 'A.h' to be included in 'B.h' and doesn't include it
in 'B.cpp'. Later, somebody changes class B to acutally have
'a' to A*, and 'B.h' to have a forward-declaration of A instead
of including the header:

----------------------- new B.h
class A;
class B {
A* a;
};
--------------------------------

Now, the build process is broken because 'B.h' doesn't provide
the definition of class A any longer.

Yes, such a problem is usually pretty close on the surface and
you can probably find and fix it relatively easily. But what if
it's some kind of macro that if defined alters the way the code
compiles but doesn't produce an error if not defined?

Just my $0.02

Victor
Jul 22 '05 #6
"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hy********@news.boeing.com...
I generally put all the includes in the header files, and have the .cpp
files only include their corresponding .h files. Part of the reason is that duplication is bad (if you have to add a header file, you have to add it to two separate places).


That approach has the following consequence: if your implementation changes
and requires a new header that is not already included in your .h file, you
have to change this .h file, which will force re-compilation of all other
dependent modules instead of just your .cpp file as it should. If you only
include in your .h file what this .h file directly needs, the problem does
not occur.
Jul 22 '05 #7

"cppaddict" <he***@hello.com> wrote in message
news:oj********************************@4ax.com...
Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires string. Is it
good form to repeat the <string> include to make the dependency
explicit, or do just allow the include to be make implicitly through
the .h include? That is, should the header of your .cpp file be:

#include "CustomClass.h"

OR:

#include <string>
#include "CustomClass.h"


If the use of string is independent, it's best to include it again. For
example, if the use of string were for a parameter of a member function,
then of course you would definitely *not* want to use your second example.
That creates bad dependencies on the order of includes, etc. If it's
something used for implementation only, it should be included again. That
way if anything about the interface changes wrt needing the string
definition, then you only need remove it from the h file. It is also more
clear in terms of "self documentation".
Jul 22 '05 #8
cppaddict wrote:
Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires string. Is it
good form to repeat the <string> include to make the dependency
explicit, or do just allow the include to be make implicitly through
the .h include? That is, should the header of your .cpp file be:

#include "CustomClass.h"

OR:

#include <string>
#include "CustomClass.h"

thanks for any thoughts,


Per /Large Scale C++ Software Design/ by John Lakos, the first statement in
CustomClass.cpp should be #include "CustomClass.h". (Precompiled header
systems, like VC++'s "stdafx.h", may go above.)

If AnotherClass.cpp needs to use CustomClass, it should only need to include
its header. There should be no surprise dependencies. If CustomClass.h needs
std::string (likely), then #include <string> should be inside it.

But if CustomClass uses BigClass, it should try not to #include
"BigClass.h". It should instead forward declare what it needs like this:

class BigClass;
class CustomClass {
void method(BigClass & aBigClass);
...
};

Then, CustomClass.cpp will #include "BigClass.h". But any other class that
needs CustomClass but doesn't need BigClass won't #include "BigClass.h", and
won't see any of its identifiers. C++ has many scratchy parts, but it
matches logical to physical decoupling very well. One can partially
recompile small modules of large applications typically without accidentally
recompiling everything.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #9
cppaddict wrote:
Say that your CustomClass.h header files requires

#include <string>

Now say that your CustomClass.cpp file also requires string.
Is it good form to repeat the <string> include
to make the dependency explicit?
Or just allow the include to be make implicitly through the .h include?
That is, should the header of your .cpp file be:

#include "CustomClass.h"
Yes.
OR:

#include <string>
#include "CustomClass.h"


No.

Your CustomClass.h header file
is your public *interface* to your CustomClass class.
If it requires the <string> header to be *self-sufficient*,
then it isn't necessary to include it again in your source file.

If, on the other hand, the <string> header is required only
by the *implementation of* and not the *interface to* your CustomClass,
you should include the <string> header *only* in your CustomClass.cpp
source file and *not* in your CustomClass.h header file.
Jul 22 '05 #10
qWake wrote:
Joe Laughlin wrote:
I generally put all the includes in the header files, and have the .cpp
files only include their corresponding .h files. Part of the reason is
that
duplication is bad (if you have to add a header file, you have to add it


to
two separate places).


That approach has the following consequence:
if your implementation changes and requires a new header
that is not already included in your .h file,
you [must] change this .h file,
which will force re-compilation of all other dependent modules
instead of just your .cpp file as it should.


No.
If the .cpp source file requires a new header
that is not already included in your .h header file,
it should be included *only* in the .cpp source file.
If you need to include it in your .h header file as well,
it means that you must have changed the interface.
If you only include in your .h file what this .h file directly needs,
the problem does not occur.


Correct.
Jul 22 '05 #11
Joe Laughlin wrote:

I generally put all the includes in the header files, and have the .cpp
files only include their corresponding .h files. Part of the reason is that
duplication is bad (if you have to add a header file, you have to add it to
two separate places).


It's only necessary to add it twice if it's *used* in both the header
and the .CPP file. I could write a library that uses, say, std::string
in its *implementation*, but not as part of its *interface*; i.e. none
of the exposed functions accept or return std::string. In this case,
personally, I would #include <string> in the .CPP file, not in the header.

--
Mike Smith

Jul 22 '05 #12

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

Similar topics

2
by: Bruce W...1 | last post by:
For include files I see both the file extensions .php and .inc used. Which one is proper? Does it matter? What's the diff? Thanks for your help.
59
by: Haines Brown | last post by:
I've not had a clear definition in my mind of "citation," and so have avoided it. For example, if I suggest that the reputation of the New York Times has suffered, is that a citation? I suppose...
9
by: Richard Silverstein | last post by:
I was hoping someone more expert in css could help me w. a problem I'm having w. a stylesheet I've created for a Pbase photo site. My site is at http://www.pbase.com/richards1052. Currently, it...
0
by: Colin Steadman | last post by:
I've work out how to use the filter gradient using this link: http://msdn.microsoft.com/workshop/author/filter/reference/filters/gradient.asp But I cant convert it into proper CSS, could anyone...
14
by: Viken Karaguesian | last post by:
Hello all, It stinks being a newbie! One thing that I'm not sure about with CSS is: where is the "proper" place to put font attibutes? This has me a little confused. Take the below style sheet...
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
0
by: bettervssremoting | last post by:
To view the full article, please visit http://www.BetterVssRemoting.com Better VSS Remote Access Tool including SourceOffSite, SourceAnyWhere and VSS Remoting This article makes a detailed...
9
by: Arancaytar | last post by:
I have so far seen two methods for including external resources as CSS stylesheets in a document. The first is this: <link href="/stylesheets/style.css" rel="stylesheet" type="text/css" /> And...
2
by: Albin | last post by:
Hi, I have a html page where in the table spans for two pages the first page's last row doesn end properly and the 2nd page's first row also isnt proper.The code i use is given below,the no of...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.