CondorMan,
Are you confused by C++ namespaces or by the C++ way of using the using keyword?
I'm asking this because of this sentence in your post:
Quote:
Logic says to me that I should only have to mention std namespace as it seems "higher" than <iostream>
One way to understand the using keyword lies in one of the premise of the C++ language:
Thou shall not use any undeclared symbol
As BeanDog told you, the #include
preprocessor directive is used to paste the content of a file into another file. In a sense, it imports the declarations that are in the included file into the includer file. You can see the output of the preprocessor by doing this (.NET 2003, but should be very similar ofr .NET 2005):
- go to the project properties
- in the C++ properties, chose 'Command Line'
- in the Additional Options text bos, add
/P / EP
(/P = write preprocessor output, /EP = don't write #line directives in the preprocessor output)
- Click OK
- Compile your project (the project will not link, because the object files are NOT created)
- go to your source (probably Debug or Release)
- the preprocessor output have the .i extension
Don't forget to revert this project options changes if you want to be able to successfully compile and link your project. Be aware that the generated files are rather big (often several thousands of lines).
The #include directive has no real impact on the compilation - in fact, strictly speaking, it takes place before compilation, in a step called preprocessing. The goal of preprocessing is to prepare a
compilation unit (the resulting .i file) to be compiled. In fact, #include is a helper because it allows you to put the declarations that might need to be used in more than one C++ file into a single file (a header file - most of the time they have a .h extension, but some of them (those of the Standard C++ Library) may have no extension). Then, instead of writing the declaration in each C++ file, you simply include the corresponding header file.
This way, you can import the declarations you need in your compilation unit.
Now that all needed symbols are known, we are able to use them. Before I continue, let speak a bit about namespaces and their usage.
Namespaces are relatively new beasts in software engineering. The current trend is to use namespaces to create packages of classes that share a same goal (for example, providing a standard library to C++ programmers). You can even add subnamespaces (to create subpackages).
The strict usage of fully qualified names may lead to source code that is difficult to read - and difficult to maintain / modify. This is expecially true when namespace names are long or when you are using symbols that are declared in subnamespaces. For example:
company::database::clientmanager::Client *client = new company::database::clientmanager::Client( company::database::clientmanager::ClientType::VERY_RICH);
Now that I wrote this horror, the goal of the using keyword should become clearer: while namespace are really useful because they help the definition of clearly bound units, they can lead to barely readable or even unreadable source code, which is never a good thing. the using keyword allows you to simplify the code when you need to use symbols that have been declared in a namespace.
using company::database::clientmanager;Client *client(ClientType::VERY_RICH);
This is easier to read, isn't it?
You may ask: why did he speak about #include, then namespaces, then using? That's because I feel that you misunderstood the roles of #include and using: while you beleive that they are related, in fact they are not related at all (they can't be related, because they are used in different steps of the whole compilation process). As BeanDog clearly stated, #include is a copy-paster that allow you to declare your symbols in one file and to use this declaration in many classes (hence to satisfy the first part of the C++ premise that I stated). The using keyword allows you to define a new, simpler way, to gain access to these declared symbols (hence to satify the other part of the same C++ premise).
In your example, #include <iostream> is used to import the declarations of the streams symbols, and using std; is used to simplify the access to the symbols that have been imported and that are in the "std" namespace.
I hope I have been clear enough ;)
Regards,