| I l @ ve RuBoard |
9.2 Namespaces
The
One solution is to use
This works after a fashion, but things get a little hairy as more and more modules are added. When you're dealing with the
The C++ solution to this problem is to divide the program into namespaces. You deal with namespaces every day in real life. For example,
C++ lets you define something called a
namespace
. All the variables declared inside a
namespace
are
namespace display {
int width; // The width of the display
int height; // Height of the display in lines
bool visible; // Is the display visible?
};
A family member's full name might be Stephen Douglas Smith. The C++ equivalent of a full name is something called a fully qualified name. In this case, the fully qualified
9.2.1 Namespace stdWe started out using the object std::cout for output. What this actually means is that we are using the variable cout in the namespace std . This namespace is used by C++ to define its standard library objects and functions.
You may remember that we
#include <iostream> The first statement causes the compiler to read in a file called iostream, which contains the definitions of the C++ standard variables. For example, a simplified iostream might look like this:
namespace std {
istream cin; // Define the input stream cin
ostream cout; // Define the output stream cout
ostream cerr; // Define the standard error stream
// Lots of other stuff
}
Once the compiler has seen these definitions, std::cin , std::cout , and std::cerr are available for our use. 9.2.2 Global NamespaceIf you do not enclose your code or variables in any namespace, a blank namespace is assigned to them. For example, the expression: ::global = 45;
9.2.3 File-Specific NamespaceLet's suppose you want to define a module and you want most of the functions and variables in the file to exist in their own unique namespace. You could put the following statement at the top of your file:
namespace my_file_vars {
But what happens if, by some
To avoid this, C++ has invented the unnamed namespace. The declaration:
namespace {
with no name specified, puts all the
9.2.4 Nested Namespaces
Namespaces may be nested. For example, we could declare some variables as
namespace core {
namespace games {
namespace dice {
int roll; // The value of the last roll
Nesting this deep is a little verbose ( core::games::dice::roll ), but if you have a lot of code to organize, nested namespaces may be useful. 9.2.5 The using Statement
Let's assume we have a program with a command module (with the namespace
command
) and a command parsing module (with the namespace
command_parser
). These two modules are very closely
When writing the command module, if you want to refer to a variable in the parsing module, you have to prefix it with the namespace identifier: if (command_parser::first_argument == "ShowAll")
Because these modules are tightly
But you can tell C++, "I know that
first_argument
is in the
command_parser
module, but
using command_parser::first_argument; C++ will now let you use the name first_argument instead of command_parser::first_argument . using command_parser::first_argument; if (first_argument == "ShowAll")
Now let's suppose there are a lot of variables that we wish to import from the module command_parser . We could put a using statement in our code for each one, but this would require a lot of statements. Or we can do things wholesale and tell C++ that all the names in the namespace command_parser are to be imported into our module. This is done with the statement: using namespace command_parser; 9.2.5.1 The problem with the using statementThe use of the using statement should be avoided in most cases. The example we presented here had many interconnects between the two namespaces which necessitated the use of the using statement. But it's considered bad program design to have so many interconnects. The using statement also causes namespace confusion. Normally if you see a variable without a scope declaration (e.g., signal_curve ) you can assume it belongs to the current namespace. If there are using statements in the program, this assumption is no longer valid and your life just got more complex. Programs are complex enough already, and this complication is not welcome. |
| I l @ ve RuBoard |