Generally, my projects go into one large file. The few times it didn't was when I was using class-based things which make sense to separate and where it is obvious what goes into the header and what goes into the source file.
Now, however, I find myself struggling to keep things organized, thus I want to spread a wide range regular functions over separate files. Two questions arise:
a) I have a list of 'general' functions that don't require a class. What is the proper way to move-and-reinclude them? Do I still require both a header and source file?
b) I have a list of functions that access variables I intend to declare globally in the main file. I can pass them to the functions by reference, but the list gets long and some of these functions are used very frequently. How can I make them accessible to other files?
a) I have a list of 'general' functions that don't require a class. What is the proper way to move-and-reinclude them? Do I still require both a header and source file?
When I'm writing a little app, I start off by putting unconnected, useful little functions into utils.h and utils.cpp
The utils.cpp is needed for the bigger function; shorter functions I declare as inline in the header. So the .cpp file is not always required.
When I get enough functions, I split them up into group, e.g. string_utils.h/.cpp; cmdline_utils.h/.cpp; ...
I use namespaces to group the functions. But you could use utility classes [1] instead, which contain the functions as static member functions.
b) I have a list of functions that access variables I intend to declare globally in the main file. I can pass them to the functions by reference, but the list gets long and some of these functions are used very frequently. How can I make them accessible to other files?
If you really must be able to access global variable from other .cpp files, you need to declare them as extern in a header file.
// main.cpp
#include <iostream>
...
usingnamespace std;
#include "main.h"
int g_evil = 666;
constchar* g_esp_evil = "spawn of the devil";
...
int main()
{
doEvil();
...
return 0;
}
But you really shouldn't have globals!
Are you familiar with structs? You could group related variables using structs, and then you just have to pass a reference to a few struct rather than loads of separate variables.
I know globals are generally considered bad programming practice, but I'm programming for purely functional reasons. Chances are nobody else will see, let alone expand on, my code. Globals 'cramping up' the code is the last of my worries.
However, the main reason I'm avoiding struct-grouping is because nearly every function uses one or more globals, so a) I'd often be passing a full struct while only a single vector is required (which might not make any difference in performance?) and b) adding the extra step of "access this var in this struct" causes extra overhead. As most of the code consists of a single matrix being manipulated by a large amount of functions, adding that extra struct-step might have a significant impact on performance.
I'll go with the 'extern' option for now, but I'll keep structs in mind. Some of the 'smaller' globals can (and probably should) be grouped anyway, as one can't appear without the other(s).