Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script for separating implementation from headers in a .h file

Tags:

c++

header

Sometimes, when working with small classes, it's a pain in the ass to separate the implementation in a .cpp file, so I put all the code in the header file (kids, don't do this).

However, eventually these classes grow big time and I have to make the separation that I didn't do in the beginning.

The fact is that this is a mechanic process that could be perfectly done by a script or something like that, and I'm sure someone out there has already thought of this.

So, do you know of any script that could get a .h file, with all the function bodies implemented, and then return a stripped .h file and a nicely filled .cpp file?

like image 662
José Tomás Tocino Avatar asked Mar 16 '11 17:03

José Tomás Tocino


People also ask

Can you implement methods in a .h file?

In C, you cannot have the function definition/implementation inside the header file. However, in C++ you can have a full method implementation inside the header file.

Are .h files compiled?

Only source files are passed to the compiler (to preprocess and compile it). Header files aren't passed to the compiler. Instead, they are included from source files.

Can we define a function in .h file?

The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.


1 Answers

I'm guilty of this as well - although I've never really thought about automating the split before, as there are usually related project and test files to touch.

The idea of automating this is interesting, though. The most promising tool I see out there is this one:

http://os.inf.tu-dresden.de/~hohmuth/prj/preprocess/

like image 82
holtavolt Avatar answered Nov 03 '22 09:11

holtavolt