I wonder why this C ++ code is valid and does not cause any errors:
extern int B;
int A = B;
int B = A;
int main()
{
printf("%d\n", B);
system("pause");
return 0;
}
Firstly, the variable Awill be created at some memory address, then its value will be initialized from the variable B, but then the variable will Breturn to initializing its value from the variable A, and therefore on, ...
So, why is there no infinite loop or any error ?
The program still works fine, and the value Bis 0.
This is also true for Java:
class A {
static final int AA = B.BB;
}
class B {
static final int BB = A.AA;
}
Anyone can explain these issues to me, thanks!
source
share