What does this mean ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What does this mean ?

int x; x = 5; cout << ::x;

29th Aug 2018, 2:35 PM
Amr Mostafa Abbas
Amr Mostafa Abbas - avatar
5 Answers
+ 7
was this part of the code inside of the function or not? Either way, it will cause an error i guess you know what first 2 lines do, lets move to 3rd one. " :: " is a scope resolution operator, it searches for the thing to the right of it inside the thing to the left of it. You normaly see it when you start working with classes or namespaces. If there is nothing to the left of it, it means that it searches for variable x in the global scope (in case of variables that means it searches for global variable) but here is what confuses me... If this bit of the code was inside a function, then ::x would cause an error, because x is not global (unless there is one global variable named x which you didnt include in the post). If it wasnt inside a function, then x = 5 and cout would cause an error
29th Aug 2018, 3:10 PM
Data
Data - avatar
+ 2
:: is something like range operator. it reaches outside of the scope to get the x value, so for example if you make new x value in a local scope but there is variable called x outside of that scope you can reach for that variable using ::
29th Aug 2018, 3:09 PM
Paul
+ 1
create a variable named "x" of the type INT. assign value 5 to variable x. write x to the console
29th Aug 2018, 2:53 PM
Paul
+ 1
thanks for all..
29th Aug 2018, 3:16 PM
Amr Mostafa Abbas
Amr Mostafa Abbas - avatar
0
::x ??
29th Aug 2018, 2:54 PM
Amr Mostafa Abbas
Amr Mostafa Abbas - avatar