This is the subject code:
externfile.cpp
int i = 10;
mainfile.cpp
#include <iostream>
using namespace std;
extern int i;
int main(int param)
{
cout << i << '\n';
int i = 0;
cout << i << '\n';
cout << ::i << '\n';
}
When compiling this program (using Visual Studio 2008), it works fine and the output will be:
10
0
10
which is not surprising for me, this is a matter of scopes.
But what makes me confused is: How could mainfile.cpp
file get the value of i
from another .cpp
file (externfile.cpp
in our case)? is it only because they reside in the same directory? or the same solution?
And in a better way: How are source files "merged" while compiling a project (with VS2008, if I should specify)? in what "order" are they placed? and how are they scoped?