#include <iostream>
using namespace std;
int d = 10;
int main()
{
int d = 20;
{
int d = 30;
cout << d << endl << ::d; // what does it mean?
}
return 0;
}
output is:
30
10
I don't understand why "::d" gives 10? Can someone explain it to me please?
::d means d from global namespace
EDIT: There are three different variables with similar name d. One is in global namespace d=10, one is inside scope of main function (20), and the last one is inside internal block of the main function (30).
Inside every block you have access (by name) to corresponding variable and always have access to the global namespace (by ::).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With