June 2025
Intermediate to advanced
1093 pages
33h 24m
English
For exactly this purpose, you can also use the keyword static. Instead of opening an anonymous namespace, you write static before the variables and functions. You achieve the same thing: the name of the variable or function is only visible within this module. Other modules can use the same name without the compiler complaining.
It would look like this for modul.cpp:
// https://godbolt.org/z/zfYzPjqrW// modul.cpp#include "modul.hpp"static std::string PREFIX = "TREE:";static void printInfo(std::ostream& os) { os << "Author: Torsten T. Will\n";}bool debug = false;// rest as before
The debug variable is not yet static. As a result, no other module can define a global function or global variable debug, but that is intentional ...
Read now
Unlock full access