Include Coding Standard

Hi all,

my question is quite easy, but i cant really find a yes or no answer.

when including files where would you generally put your includes?

Example One:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a.h:
#pragma once
#include <string>
#include <vector>

#include "b.h"

class a
{
//..... code
}

a.cpp
#include "a.h"

//.....code 


Example Two:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a.h:
#pragma once
class a
{
//..... code
}

a.cpp
#include <string>
#include <vector>

#include "a.h"
#include "b.h"

//.....code 


so which would be the better choice?
Last edited on
Example Two is better. You should generally avoid having to depend on libraries needed by your program to be declared in other files. The main file has #pragma once enabled; so even if you declare all the libraries needed by main program in, in anther file, including them in your main will not incur any overhead
so if i understand correctly you need to do all the includes in the cpp files?
yes, you can also include them in the header files, but they should all be included in the main file
I go with
a.h:
1
2
3
4
5
6
7
#ifndef INCLUDED_A_H
#define INCLUDED_A_H
class a
{
//..... code
}
#endif // INCLUDED_A_H 


a.cpp
1
2
3
4
5
6
#include "a.h"
#include "b.h"
#include <string>
#include <vector>

//.....code  


(pragma is non-standard, and I include project files before library files to expose hidden dependencies)
Topic archived. No new replies allowed.