Making a Library Changes Your Names
Remember the function Initialize(), which was used to prepare cin for throwing exceptions? What if you moved error handling out to a module of its own, and some other library or main program wanted to use that common name? This could be a problem.
You have already seen a way to deal with this possibility: namespaces. That's the solution iostream uses and it will work perfectly for this situation.
The form of a namespace declaration is
namespace namespacename { statements } ;
Listing 8.3 is the new header file.
Listing 8.3. Header for ErrorHandlingModule
1: #ifndef ErrorHandlingModuleH 2: #define ErrorHandlingModuleH 3: *4: namespace SAMSErrorHandling *5: { 6: void Initialize(void); 7: int HandleNotANumberError(void); ... |
Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.