// unnamespace-main-1-2.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates unnamed namespaces. // Sometimes, it is desired to define an object, a function, a class type, // or any other entity to be visible only to a small portion of the program; this // will consequently reduces the namespace pollution problem. Since the entity is // not going to be used outside this program (or a portion of it), it is better to // achieve this using a different method (using namespaces would be intended to // avoid collision with other similar names in other files, which is not the case here). // // One way to limit the scope of an entity could be achieved by declaring the variable // inside a function (such as local variables for example). However, what if this entity // is to be seen by multiple functions, without making this method visible to the // entire program? // In this case, unnamed namespaces can be used. // this program does not in fact show how unnamed namespaces can be used; rather why // they are (could be) needed. Notice that this program does NOT work. #include "unnamespace1.h" #include "unnamespace2.h" int main() { fun1(); fun2(); fun3(); fun4(); fun5(); fun6(); fun7(); fun8(); cout << endl; return 0; } /* The result of compiling & linking the program Compiling... unnamespace-main-1-2.cpp unnamespace1.cpp unnamespace2.cpp Linking... unnamespace2.obj : error LNK2005: "void __cdecl greeting(void)" (?greeting@@YAXXZ) already defined in unnamespace1.obj Debug/namespace-main-1-2.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. namespace-main-1-2.exe - 2 error(s), 0 warning(s) */