namespace android {
extern int i; // declare here but define somewhere
void foo ();
}
-- is used for scoping variables and functions inside a particular name. While using/calling those variables/functions, use scope resolution operator ::
. e.g.
int main ()
{
android::foo();
}
There is no restriction for putting all namespace
declarations in a single body instance. Multiple namespace android
bodies spread across several files, is possible and also recommended sometimes. e.g.
// x.cpp
namespace android {
void somefunc_1 ();
}
// y.cpp
namespace android {
void somefunc_2 ();
}
Now, sometimes you may find using ::
operator inconvenient if used frequently, which makes the names unnecessarily longer. At that time using namespace
directive can be used.
This using
directive can be used in function scope / namespace scope / global scope; But not allowed in class
scope: Why "using namespace X;" is not allowed inside class/struct level?).
int main ()
{
using namespace android;
foo(); // ok
}
void bar ()
{
foo(); // error! 'foo' is not visible; must access as 'android::foo()'
}
BTW, Had using namespace android;
declared globally (i.e. above main()
), then foo()
can be accessed without ::
in Bar()
also.