How can I make visible variables/functions in particular files?
For example, lets say I have this hierachy of files:
a.h
extern int var;
a.cpp
#include "a.h"
int var;
b.h
#include "a.h"
void function();
b.cpp
#include "b.h"
void function() {
var = 0;
}
in main.cpp I want to be able call function(), but not to access var variable
#include "b.h"
int main(int argc, char** argv) {
function(); /* possible to call */
var = 0 /* var shouldn't be visible */
}
I don’t want file a.h to be included in main.cpp – only b.h. How can I achieve this?
a.h doesn’t need to be included in b.h, only b.cpp. This is because
varis only needed by the function definition, not its declaration. This goes along with the rule not to include headers in other headers unless you absolutely have to.b.h
b.cpp