+ 2
Why the output of the following code is not 41?
why using namespace does not forces nm::x to be used? https://code.sololearn.com/cEN8GyC6UP27/?ref=app
6 odpowiedzi
+ 2
The variable x defined in the main function hides the name defined in the namespace N. As guidence, a name may be hidden by inner scope defined by a namespace.
If you want to refer to the variable in the N namespace you should use the scope resolution operator like so: N::x.
Also, if the variable was defined in a global scope you could refer to it like so: ::x.
As a side note: This is the main reason why the use of using namespace YOURNAMESPACE is considered bad practice as it can lead to unexpected behaviors.
+ 5
You need std::cout<<nm::x; to output 1. A local variable seems to take precedence over a variable of the same name in a name space that is being used.
+ 2
Another solution would be to limit the scope of the variable defined in main like shown here.
https://code.sololearn.com/csWUHqqTKrD3/?ref=app
0
The output of 44 only not 41 vedansh Mittal
- 3
Llwlwpa ap
P20wlp
S