From what I have gathered, the 'extern' keyword in c++ can be used to tell the compiler that a variable is defined in another .cpp file. I was wondering if this definition had to be explicit, or if the definition could be changed via side-effect by a function in the .cpp file where the variable is defined.
i.e.
//a.h
extern int foo;
//a.cpp
#include <a.h>
int foo=0;
int func(int &foo) // EDIT: oops, forgot the type for the parameter and return statement
{
foo = 10;
return 5;
}
int x = func(foo); // EDIT: changed to match declaration and assigned function to dummy variable
//b.cpp
#include <a.h>
int main()
{
cout << foo;
return 0;
}
Can the program recognize that foo should be 10, or will it be 0? And if the compiler recognizes foo as 0, is there a way I can make it so that it recognizes it as 10? Also, the reason I can't just compile and test this myself is that I'm not sure how to compile when there are multiple files, I'm new =).
EDIT: Thanks for the error pointers, but I guess the main question is still if b.cpp can see if foo is 10 or 0. cheers!