Namespaces

If you use iostream instead of iostream.h, you should use the following namespace directive
to make the definitions in iostream available to your program:
using namespace std;
This is called a using directive. The simplest thing to do is to accept this for now and worry
about it later (for example, “Memory Models and Namespaces”). But so that you
won’t be left completely in the dark, here’s an overview of what’s happening.
Namespace support is a fairly new C++ feature designed to simplify the writing of programs
that combine preexisting code from several vendors. One potential problem is that you might
use two prepackaged products that both have, say, a function called wanda(). If you then use
the wanda() function, the compiler won’t know which version you mean. The namespace
facility lets a vendor package its wares in a unit called a namespace so that you can use the
name of a namespace to indicate which vendor’s product you want. So Microflop Industries
could place its definitions in a namespace called Microflop. Then Microflop::wanda()
would become the full name for its wanda() function. Similarly, Piscine::wanda() could
denote Piscine Corporation’s version of wanda(). Thus, your program could now use the
namespaces to discriminate between various versions:
Microflop::wanda(“go dancing?”);          // use Microflop namespace version
Piscine::wanda(“a fish named Desire”); // use Piscine namespace version
In this spirit, the classes, functions, and variables that are a standard component of C++ com-
pilers are now placed in a namespace called std. This takes place in the h-free header files.
This means, for example, that the cout variable used for output and defined in iostream is
really called std::cout and that endl is really std::endl. Thus, you can omit the using
directive and code in the following style:
std::cout << “Come up and C++ me some time.”;
std::cout << std::endl;
However, most users don’t feel like converting pre-namespace code, which uses iostream.h
and cout, to namespace code, which uses iostream and std::cout, unless they can do so
without a lot of hassle. This is where the using directive comes in. The following line means
you can use names defined in the std namespace without using the std:: prefix:
using namespace std;
This using directive makes all the names in the std namespace available. Modern practice
regards this as a bit lazy. The preferred approach is to make available just those names you
need, with something called a using declaration:
using std::cout;  // make cout available
using std::endl;  // make endl available
using std::cin;   // make cin available
If you use these directives instead of this:
using namespace std;  // lazy approach, all names available
you can use cin and cout without attaching std:: to them. But if you need to use other
names from iostream, you have to add them to the using list individually. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值