I am working with a book on c++ and to solve the problems I am always asked to declare the member functions' prototypes in the xxxxxx.h file and define the functions body properly in the xxxxxx.cpp file. Is there any harm or disadvantage in defining the member functions in the .h file? If not, is there any benefit or advantage in defining them in the .cpp file?
-
you can't define function bodies in .h file. – sedran Aug 10 '12 at 06:17
-
Your are free to write your code as you wish without following any protocol, but it will remain good till not read by others..... – perilbrain Aug 10 '12 at 06:17
-
This is generally known as a header-only library, and you'll be better off searching for other answers with that knowledge, e.g. http://stackoverflow.com/questions/2174657/when-are-header-only-libraries-acceptable – Marc Bollinger Aug 10 '12 at 06:18
-
@sedran:- http://stackoverflow.com/questions/453372/writing-function-definition-in-header-files-c – perilbrain Aug 10 '12 at 06:19
-
damn, I didn't know that! They taught us so in the school :( – sedran Aug 10 '12 at 06:20
2 Answers
If you define the method in the header, you have to mark it as inline
or define them inside the class definition to prevent multiple definitions:
//A.h
class A
{
void f()
{
}
void g();
};
inline void A::g()
{
};
Other than that, it's a matter of coding style. You generally keep your headers clean, as to only provide an interface to work with the class, and abstract away the implementation details.
Generally. There are cases where you have to define the functions in the header (templates) or when you want to (to allow better compiler optimizations).

- 253,575
- 64
- 457
- 625
-
Have they not solved this template issue yet, with the new C++ standard? – Lundin Aug 10 '12 at 06:55
-
Writing the whole class implementation in the h file isn't an issue? It turns very messy and unreadable, and you can't provide a public interface to the caller with the h file, you have to expose the whole implementation. – Lundin Aug 10 '12 at 07:18
-
1@Lundin you can separate the implementation in a different file, but it still must be visible. – Luchian Grigore Aug 10 '12 at 07:20
-
There is also the drawback that the client code will have to be recompiled, as opposed to re-linked, after the most trivial implementation changes. – juanchopanza Aug 10 '12 at 08:25
If you will always write your code in .h
files you will never be able to performa some technics, such as forward declaration
. Since you can't use forward declaration if writing code in headers you will be not able to solve cross dependencies
.
So you will need to include everything you need in .h
file. Including this file in other file will include everything already included. Thus any small change in you code will result in almost full recompilation in many cases.
Also it's harder to read the code in .h
files because you expect to see the interface of the class in .h
, not implementation.

- 24,218
- 13
- 61
- 90
-
You can solve cross dependency issues with headers and forward declarations just fine. There's nothing magical about headers. The other problems you mention still apply, though. – R. Martinho Fernandes Aug 10 '12 at 06:28
-
but are member functions not part of the interface? It's true, forward declaration is impossible! Thanks for your help! – Aug 10 '12 at 06:30
-
@R.MartinhoFernandes: How are you going to solve cross dependency if you need to write the implementation in the same file? Forward declaration will not be enough – Andrew Aug 10 '12 at 06:32
-
@F'OlaYinka: member functions are part of course. But to use these functions you don't need to see the implementation. You can just open a header and see the function documentation. If instead you'll see an imlementation also it will be harder to find the required information – Andrew Aug 10 '12 at 06:34
-
1Erm, you just write the declarations first and the definitions (`inline`) afterwards. There's nothing different about it. – R. Martinho Fernandes Aug 10 '12 at 06:34
-
@R.MartinhoFernandes: And how will you write the definition without include? You will not be able to use the declared class/struct! – Andrew Aug 10 '12 at 06:35
-
You can put the mutually dependent classes in the same file. – R. Martinho Fernandes Aug 10 '12 at 06:39
-
He's right you know... Just define both classes before any method definitions, and define the methods afterwards, in the header. – Luchian Grigore Aug 10 '12 at 06:40
-
@R.MartinhoFernandes: Yes, it's possible to do so, but I don't think it's good to put every cross dependence in the same file in any case – Andrew Aug 10 '12 at 06:43
-
Of course it's not. Nobody said it was... But there's a long way from "it's not good" to "you can't"... – Luchian Grigore Aug 10 '12 at 06:45