int value1 = 20 ;
int value2 = 40 ;
}
//...
void f()
{
int value2 = 10 ;
using namespace Window ;
std::cout << value2 << std::endl ;
}
The output of this function is 10 , not 40. The value2in namespace Windowis hidden by
the value2in f(). If you need to use a name within a namespace, you must qualify the
name with the namespace name.
An ambiguity can arise using a name that is both globally defined and defined within a
namespace. The ambiguity surfaces only if the name is used, not just when a namespace
is introduced. This is demonstrated with the following code fragment:
namespace Window
{
int value1 = 20 ;
}
//...
using namespace Window ;
int value1 = 10 ;
void f( )
{
value1 = 10 ;
}
The ambiguity occurs within function f(). The directive effectively brings
Window::value1into the global namespace; because a value1is already globally
defined, the use of value1in f()is an error. Note that if the line of code in f()were
removed, no error would exist.
The usingDeclaration....................................................................................
Theusingdeclaration is similar to the usingdirective except that the declaration pro-
vides a finer level of control. More specifically, the usingdeclaration is used to declare a
specific name (from a namespace) to be in the current scope. You can then refer to the
specified object by its name only. The following example demonstrates the use of the
usingdeclaration:
namespace Window
{
int value1 = 20 ;
int value2 = 40 ;
650 Day 18