My header files' implementation

I make my own header files in my free time for my own use. For eg. I made luint.h for handling very large integers and ekumath.h for some useful math functions.

I've seen some people on the internet talk about something like 'splitting interface and implementation' or 'splitting declaration and implementation'. They say that .h files should only be used for declaration and implementation should be in a .cpp file.

If I write the implementation in the .cpp file, how will my main code know about it? I will either have to include the .cpp file in my project or #include it. But these ideas seem to defeat the purpose. I think it should work like the C libraries; just #include the header file and you have the implementation (whether it's binary or .cpp).

Can anyone please explain this concept with its advantages or disadvantages?

(I use Visual Studio 2008 Express on windows 7)
Last edited on
You should add cpp into your project. Or you can compile it and add it in the linker options fo other project, like third party libraries.

Main problem with having interface and implementation in one file arises when you need to use your header in more than one file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//sample.h
void do_nothing(){}

//main.cpp
#include<sample.h>
int main()
{
    secondary::calculate();
    do_nothing;
}

//secondary.cpp
#include<sample.h>
namespace secondary
{
    void calculate()
    {
        do_nothing()
    }
}

Linker will throw an error that function do_nothing() declared twice: in main.cpp and secondary.cpp
just stick in a header guard, and it'll only be included once.
http://en.wikipedia.org/wiki/Include_guard

just stick in a header guard, and it'll only be included once.
No. It prevents header from including twice in one translation unit (cpp file). It wont prevent multiple definition case I described above.
@miinipaa: your example is terrible. It doesn't even compile. plus by doing this:

void do_nothing(){}
you are implying implementation in the header, due to the presences of the braces.

OP: use this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// sample.h - interface only
class myClass
{

public:
	void DoNothing();
};

// sample.cpp - implementation
//secondary.cpp
#include "sample.h"


void myClass::DoNothing()
{
	// implementation code in here
}

//main.cpp
#include "sample.h"

int main()
{
	myClass myClassObject;
	myClassObject.DoNothing();

	return 0;
}
you are implying implementation in the header

I have intended to show why you should separate interface and implementation. Try to read my posts again.
Last edited on
and you've put implementation (i.e. the do_nothing() implementation) in the header file? So you're giving OP an example of what not to do? Makes more sense then I guess.
I quote that for you:
Topic starter:
I made luint.h for handling very large integers and ekumath.h for some useful math functions.

I've seen some people on the internet talk about something like 'splitting interface and implementation' or 'splitting declaration and implementation'. They say that .h files should only be used for declaration and implementation should be in a .cpp file.

I assumed he made both declaration adn implementation in a single file, so i wrote:
Main problem with having interface and implementation in one file arises when you need to use your header in more than one file
[...]
Linker will throw an error that function do_nothing() declared twice: in main.cpp and secondary.cpp
I made an example where that approach will lead to some errors which can be hard to catch.
Last edited on
Makes sense now sorry :)
learn to read program comments
Topic archived. No new replies allowed.