inline member functions in source file and not in header file

Hi,
I have a project with a lot of inline funcions, and I would like to remove them from the header file and put them in the source file.
I knew that this wouldn't work if I just moved the functions from one file to the other, because these funtions are called from other files (they're mostly get/set stuff), so I came up with this:

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
29
30
31
32
33
34
35
// inlinesTest.hh
#ifndef ALREADYINCLUDED
#define ALREADYINCLUDED

class myTest{
  void f1();
  void f2();
}

#ifndef INLINED
#define INLINING
#include "inlinesTest.cpp"
#undef INLINING
#endif

#endif

// inlinesTest.cpp
#include "inlinesTest.hh"

#ifndef INLINING

void ha::f1(){
//some code
}
#endif

#ifndef INLINED

inline void ha::f2(){
//some more code
}

#define INLINED
#endif 


It compiled and passed some minor tests I made, but I'd like to know if doing this would be bad practice, and if you guys have anything to say about it.

Thanks!
A header should not include cpps. Mainly because a header should not define code, but also because a cpp should include its corresponding header, and this could create an infinite inclusion. I see that you made a mess of inclusion guards because of that.
A header should not include cpps. Mainly because a header should not define code...

I know that, but inline functions that are used in many files must be defined in the header file or in all files in which they are called. The later solution is no good at all, since each time you make a change in the function you have to do it in all files, and the former is very annoying because, well, the definition is in the header file...

but also because a cpp should include its corresponding header, and this could create an infinite inclusion. I see that you made a mess of inclusion guards because of that.

I know it is messy, but doesn't it solve the infinite inclusion problem?

All I want is that the declatarions stay in the header file and the definitions stay in the cpp, but I happen to have inline functions that are used from many files.
Do you have a better solution?
By definition inline functions belong in header files --not .cpp files.

If you want to keep the code a secret, it cannot be inline. Otherwise just put it in the header file.

Hope this helps.
By definition inline functions belong in header files --not .cpp files.

I see... I've read in some places that the inline keyword is a sugestion to the compiler, and that sometimes the compiler makes your functions inline even if you don't specifically tell it to do it (by using the keyword or defining the function in the class definition). So if I define a function in the cpp file and not include this file in other files where the funtion is called, will that guarantee that the function will not be made inline?

Speaking of inline functions, do you guys know of any script that removes definitions of function members from class definitions and put it outside (possibly in another file?)
Yes, inline is always a suggestion. You can mark functions you would prefer to be made inline, but the compiler is allowed to ignore you.

I don't know about guarantee, but I think it is a pretty safe assumption that no such function will be made inline.

I don't know of any such script. You could make one though. ;-)

Hope this helps.
Topic archived. No new replies allowed.