Vije
February 26th, 2003, 02:01 AM
What is empty namespace for?
namespace
{
class A
{
.......
......
};
class B
{
......
.....
}
};
namespace
{
class A
{
.......
......
};
class B
{
......
.....
}
};
|
Click to See Complete Forum and Search --> : namespace Vije February 26th, 2003, 02:01 AM What is empty namespace for? namespace { class A { ....... ...... }; class B { ...... ..... } }; Andreas Masur February 26th, 2003, 02:54 AM Namespaces allow you to group a set of global classes, objects and/or functions under one name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces. Sometimes you just want to group something within a namespace which should not known outside the local context. In these cases it would be useless to come up with a unique name, which might collide with another one. In this case you can use unnamed namespaces. namespace { class A { }; } To be able to use the grouped members of an unnamed namespace there exists an implicit 'using' directive. The former example is equivalent to namespace $$$ { class A { }; } using namespace $$$; '$$$' is a different name in every scope an unnamed namespace is declared. In addition unnamed namespaces are different for each translation unit. Therefore there is no possibility to access members from an unnamed namespaces from different translation units. Graham February 26th, 2003, 03:59 AM Basically, and unnamed namespace serves the same purpose as declaring static objects at file scope: it keeps them local to the file and unusable outside it. /* oldstyle.c */ static int local_global; static void local_function(); /*------------- */ // newstyle.cpp namespace { int local_global; void local_function(); } // ----------------- In both cases, "local_global" and "local_function" are available for use within the file (technically, "translation unit") that they are declared in, but not outside of it. When using C++, prefer unnamed namespace to static declaration. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |