+ 1
C++ :: meaning
int zzz (int a, int b) { return ((a>b)? a : b); // it's not obvious that something is smaller than a=-1 so is b returned? } int a = 1-1; int main () { int a = -1; a = zzz (a, ::a); // what's going on here? cout << a; return a; } Why is the output 0? It doesn't seem right that b is returned from line 2 because we don't know its value...
4 odpowiedzi
+ 6
Just like Aayush Saini said "::" is known as scope resolution operator. In this case it is used to access global variable ("a" declared outside the main() )
By default local variables are prefered over global so at line ' a = zzz(a,::a) '
will look like :-
a = zzz(-1,0)
And as 0 is greater than -1 thus output is 0.
+ 2
This is scope resolution operator
https://www.google.com/amp/s/www.geeksforgeeks.org/scope-resolution-operator-in-c/amp/
Surf it
+ 2
:: - scope resolution operator.
Used to tell compiler that the 'a' variable we are referring to is the one declared globally.
Here b is 0. Look at the 'a' variable before main() function declaration.