So I was looking through a few old C++ Test Books and I found the solution to one of the questions very cool! I have never seen this "syntax" before and wanted to ask if anyone knows how it actually works and why it isnt taught widely!
Question: Give the output to the following code ->
int g =10; //TAKE NOTE OF THIS VARIABLE
void func(int &x, int y){
x = x-y;
y = x*10;
cout << x << ',' << y << "\n";
}
void main(int argc, char** argv){
int g = 7; //Another NOTE
func(::g,g); // <----- "::g" is different from "g"
cout << g << ',' << ::g << "\n";
func(g,::g);
cout << g << ',' << ::g << "\n";
}
The Output:
3,30
7,3
4,30
4,3
My question was how does the "::(variable)" syntax work exactly? It gets the variable stored outside of the main but where is that memory stored(Stack/Heap)? Can we change the value of that "Global" variable through pointers?
I thought this might allow for some really cool implementations, and wanted to share this knowledge with those like me did not know of this :)