// namespace-main-13-14.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates the name "using directives". // Namespaces were introduced with standard C++. Prior C++ implementations // did not support namespaces. As a consequence, pre-standard libraries did not wrap // their global declarations in namespaces. Since an important amount of C++ code // were written before namespaces were defined, it was not possible to just enclose // the content of these libraries in namespaces; this would simply break all old // applications. // The "using declaration" can be used to solve this problem, however if the library // is quite large and the old applications uses many of the library names, then a // large number of using declarations has to be added, which is both tedious and // error-prone. To solve this problem, the "using directive" can be used. The // utilization of using directive eases the migration between the old and the new // library versions. // The using directive begins with "using namespace" followed by the namespace name. // A using directive allows us to make all the names from a specific namespace visible // in their short form. // A using directive makes the namespace members visible as if they were declared // outside the namespace at the location where the namespace definition is located. // For example: // namespace A{ // int i, j; // } // looks like: // int i, j; // to all codes that have the following using directive in their scope: // using namespace A; // Although using directives are simple to use; with a single using directive, are // the namespace member names are suddenly visible. Although this may seem like a // simple solution to, it may introduce its won problems. If an application uses its // uses many libraries, and if the names within these libraries are made visible with // using directives, then we are back to point zero; the global space pollution problem // reappears. // Key points: 1) Using Directives #include "namespace14.h" int main() { // The following using declarations are introduced inside the main() function; as a // result they are only in effect inside this function. using namespace region3; using namespace region31; using namespace region32; using namespace region33; using namespace region4; Greeting(); Calc1 c1(30), c11(45);; c1.setValue(32); Calc2 c2; c2.setValue(c1); cout << "The value of object \"c2\" has been set to : " << c2.getValue() << endl; // use the global object - set it to the same value as c1 region4::dup.setValue(c1); // must have its fully qualified name; otherwise ambiguous cout << "The value of object \"dup\" has been set to : " << region4::dup.getValue() << endl; region3::dup(c1, c11); // must have its fully qualified name; otherwise ambiguous cout << "The value of object \"c1\" has been set to : " << c1.getValue() << endl; cout << "\n\n==============================================\n"; message1(); message2(); message3(); cout << "==============================================\n"; return 0; } // The result of running the program /* Hello from region 3. The value of object "c2" has been set to : 32 The value of object "dup" has been set to : 32 The value of object "c1" has been set to : 45 ============================================== Hello...there...That is a bit better! Right? ============================================== */