12/4/2011 1 CS1C – Advanced Programming in C++ Saddleback College Fall 2011 – J Tateyama Namespaces Topic 17 – Shrinkwrap Chapter 14 CS1C – Saddleback College Namespaces We started out in CS 1A stating that identifiers were used in C++ to name things – variables and constants As we have progressed we have named – user-defined functions – user-defined data types – classes and so on There are so many things to name in C++ it is not unreasonable to expect that several things might have the same name. A namespace is simply a way to divide items into collections so that no two items in a given collection have the same definition The standard library classes are in the std name space so we have always coded “using namespace std” to avoid coding std:: CS1C – Saddleback College Namespaces Namespace - A collection of names which are typically class definitions, variables and named constant declarations When we included directives in our programs such as #include <iostream> using namespace std; then all global identifiers in the header file iostream became global identifiers in our program – gives us the ability to use identifiers such as cin and cout in our programs If a global identifier in a program is the same as a global identifier in the header file, the compiler generates an error regarding duplicate identifiers12/4/2011 2 CS1C – Saddleback College Namespaces To help alleviate this problem of duplicate names, third-party vendors adopted the practice of beginning their global identifiers with the underscore ( _ ) This is why we have said to avoid the use of the underscore as the starting character for identifiers in our programs Problems can still occur so ANSI/ISO (American National Standard Institution / International Standard Organization) Standard C++ provides a mechanism called namespace to solve the problem of duplicate global identifiers The general form for defining a namespace in C++ is as follows namespace NamespaceName { namespace members } CS1C – Saddleback College Using Directive The using directive using namespace std; tells the compiler that the program is using the std ("standard") namespace When identifiers such as cin and cout were defined in iostream, it was indicated that they were in the std namespace If you wished to write your own versions of cin and cout for a particular application, you could omit the reference to the std namespace in your program and the compiler would not know that cin and cout defined in iostream existed Only your versions of these functions would be known. To date, we have put the using directive as a global directive. This does not always have to be the case. The scope of a using directive is the block in which it appears Using directives outside of all blocks apply to the entire file that follows the directive Review SW14-4 program CS1C – Saddleback College Using Directive Suppose we wanted to use the DoMath function from NmSpc2 and the constant HELLO from NmSpc1. What would happen if we changed to the following? { // names in this block use HELLO defined in NmSpc1 and DoMath //from NmSpc2 using namespace NmSpc1; using namespace NmSpc2; cout << HELLO; cout << endl << VAL; cout << "\n" << firstNum << " + " << secNum << " = " << DoMath(firstNum, secNum); } This would not compile because it is an ambiguous reference to a namespace12/4/2011 3 CS1C – Saddleback College Using directive We need to specify which names from each namespace we want to use in this section. This is done with using declarations. { // names in this block use the following: // HELLO defined in NmSpc1 // DoMath from NmSpc2 using NmSpc1::HELLO; using NmSpc2::DoMath; cout << HELLO; // cout << endl << VAL; cout << "\n" << firstNum << " + " << secNum << " = " << DoMath(firstNum, secNum); } CS1C – Saddleback College Large Software Project Considerations Style guides suggest that using directives should be avoided in header files because every file that incorporates that header file would also include all the namespaces defined in it. Examples of fully qualified names – std::cout – std::string Use long and descriptive names for your namespaces to make them unique Use the Alias feature to establish short aliases – namespace awesome = AWESOME_Software_MissionViejo_OC_CA_US; – awesome::CalcSomething Use the using directive for the std namespace only in the implementation file Use fully qualify names in header
View Full Document