+ 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...

27th Jul 2020, 8:44 AM
Solus
Solus - avatar
3 Antworten
+ 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.
27th Jul 2020, 8:56 AM
Arsenic
Arsenic - avatar
27th Jul 2020, 8:50 AM
Aayush $aini
Aayush $aini - avatar
+ 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.
27th Jul 2020, 8:53 AM
Lakshay Mittal
Lakshay Mittal - avatar