Actually, my book says a variable in an inner scope masks one in an outer scope.
It uses hiding in the context of inheritance. I've never heard shadowing before.
Actually, my book says a variable in an inner scope masks one in an outer scope.
It uses hiding in the context of inheritance. I've never heard shadowing before.
Shadowing, as in shadowing a parameter?
Code:void hippo(int var) { int var; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Output:Code:#include <iostream> int x = 20; int main() { int x = 50; std::cout << "Local x: " << x << std::endl << "Global x: " << ::x << std::endl; return 0; }
Code:Local x: 50 Global x: 20
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
The answer needs to be x=5 and y=21
The reasoning is very simple - scope of the variables. x takes a valude of 5 globally and hence does not get modified inside main() . However main() has local variable x and that is the one that is printed by the print statement.
Y on the other hand is also a global variable and gets modified along with "global x" and only "global y" gets printed out from the main. If there was another "local y" declared then this "local y" would get printed instead.
Oh god, masking... will the similar terms never end.Originally Posted by 7stud
http://www.icce.rug.nl/documents/cpl...lusplus03.htmlIn this code fragment the scope operator is used to address a global variable instead of the local variable with the same name. In C++ the scope operator is used extensively, but it is seldomly used to reach a global variable shadowed by an identically named local variable. Its main purpose will be described in chapter 6.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101