.cpp files and header files

hi
i am new to C++ and i am still wrapping my head around the basics.
i have learnt about headers and cpp files but im not sure about the proper use.

am i supposed to use cpp files to store all my functions? and header files to forward declare functions and constants?

and i assume the general concept is to keep functions of similar category in the same file?

im just looking for an idea of the typical usage of them. i am writing a game and things are getting very cluttered, so i think its time to tidy up my code.

thanks
Header files are for those things that other source files will need, at compile-time to have in order to use the functions and classes you want them to use. Typically, that will be class definitions, function declarations, and constant definitions.

The source files should contain everything else - which includes the actual definition of functions/methods. These things are not needed by other files at compile-time, only when linking.

As for how you organize your source code into different files, that's very much a matter of taste. What works best for you?
thanks for the reply.
i think i was over thinking it :) that makes more sense now. thanks
just been working on my project and it made more related questions pop into my head.

would you typically see no function definitions etc in the main .cpp file?
so, would you keep all functions etc in a separate file from the "int main()" function?

also, would you define variables in a header file? or keep them in the main.cpp?

thanks
Last edited on
it depends on the scope of the project.

homework / micro-projects may have some, or all, local functions and classes etc in main. For simple ease of compile, I have a number of monoliths (everything in main) that can be compiled via a shell script without a make file; these a couple of pages of code -- ugly, but I work with people that don't use c++ much and these compiled items are an add on to a program we all use, so they need to be easily moved and rebuilt by people who don't know how to deal with it ... all they have to do is copy, double click and its done.

even medium sized projects may have some few simple functions in main.

large/huge projects main usually will be pretty small and pure, from an extremely small main that does nothing beyond invoking some driver function or creating a single object and invoking one function in that. But that is 'in general' .. there may be a micro-function in main, esp if it does something small and dedicated like handle main's commandline parameters. And not everything is done 'should have' correct either. Some big projects have substantial amounts of things in main, but in a perfect world, it would be includes and a driver in main, very tight.


thanks. so would you typically declare variables in the header files too then?

although my project is a small learning project i am trying to get into good habits now and keep my projects tidy.
Generally speaking, avoid global variables as much as humanly possible. As soon as your projects get to any reasonable size, they cause more trouble than they're worth.

Constants are fine. In C++, a const variable at file scope has internal linkage rather than global linkage - which is a fancy way of saying that they're not really global variables.

But again, what you include in a header file always boils down to the question: what are other files going to need to have defined, in order to use this bit of code? If a constant is only ever used in one source file, then define it there. If, however, multiple source files are going to need to use it, then define it once, in a header file, so that everything that needs it can include it.
Last edited on
I am the odd guy out here but I define constants and variables in the math way: a constant has a known fixed value that cannot be changed. I am fine with those being global: it is not a variable, it is a constant, and by that definition it cannot be a global variable. I define a variable as storage for a value that can be changed at any time. I do not allow global variables!

This may or may not work for you. Its just how I see it.
a constant has a known fixed value that cannot be changed. I am fine with those being global: it is not a variable, it is a constant, and by that definition it cannot be a global variable.

A constant is also not global anyway, as explained above, so it's kinda irrelevant.
yes, for mechanically defined 'const' items. There are other ways to make a conceptual constant that is global, eg enum. Namespace wrapping of such things is highly recommended.
Last edited on
Any name declared in the global namespace is a global name.
ie. any name that can be qualified by prefixing the the unary scope resolution operator ​::

The outermost declarative region of a translation unit is also a namespace, called the global namespace. A name declared in the global namespace has global namespace scope (also called global scope). The potential scope of such a name begins at its point of declaration and ends at the end of the translation unit that is its declarative region. A name with global namespace scope is said to be a global name.
https://eel.is/c++draft/basic.scope.namespace#4


For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

const int x = 28 ; // global name

int main()
{
    double x = 99.99 ;

    std::cout << x << '\n' ; // 99.99
    
    // "The use of ​::​ allows a global name to be referred to even if 
    // its identifier has been hidden." - https://eel.is/c++draft/basic.lookup.qual#4
    std::cout << ::x << '\n' ; // 28 (the name x is looked up in global scope)
}
Fair point. But since we were already talking about stretching the meaning of "variable" outside its strict C++ meaning, I don't think I was out of line to do the same with "global" ;)
Last edited on
thanks for the help guys!
it seems i need to rethink how i am approaching my variables! my current project is only about 650 lines and there are already 25 global variables. 22 of them are vectors, but i assume the same rules apply to those as they are just multiple variables stored together.

the project is a game and the vectors represent things like coordinates of bullets etc, which i guess could easily be declared elsewhere.

i assume best practice of avoiding global variables is to avoid accidental multiple declarations?
Topic archived. No new replies allowed.