开发者

How do I find the 'namespace keyword' of an included file?

I'm a novice C++ programmer. How can I find out the namespace (is this the right word in this context?) for开发者_运维技巧 an include like 'iomanip' or any other? When using 'std::cout', I don't know why it's 'std' and not something else.

I hope my question is clear and worth asking.

PS: My first post here :)


How did you know that cout existed in the first place?

Because you've read the friendly manual, the language standard, a good book, or an online reference. The same applies to everything: Your documentation or reference will tell you the namespace in which you find your types.

Generally, everything that's part of the C++ standard library is in the std namespace, but some things may well be in a namespace nested within. Notable examples of nested namespaces are std::placeholders and std::chrono. But you will be told the correct namespaces if you read a good reference.

Thanks to @Potatoswatter: Other constructions that use the same scope resolution syntax are static constants of classes. For example, the class std::ios_base contains a static member type seekdir with static constant values beg, cur and end; those can be accessed via std::ios_base::beg etc. Or, since the type std::ios inherits from ios_base, via std::ios::beg etc.

In many ways, a class with only static members is just a glorified namespace, and in the early days of C++ people often used nested classes to "simulate" nested namespaces, which weren't available at the time. The scope resolution syntax is the same.


It's std because cout etc. are in the standard library, and the entire standard library lives inside the std namespace.

Other libraries will probably have their own namespace (e.g. Boost is inside the boost namespace). But you'd have to consult the relevant documentation to find out the details!


To access the functions declared in iostream library we use a namespace which is nothing but a collection of identifiers (variable names & some other type of names) that belong to a group or family..

Now std is a namespace and all identifiers in c++ standard library belong to it. There are 2 ways to refer to a specific identifier that belongs to a namespace:

  1. use the using statement at the begining of the program
  2. prefix the identifier with the name of namespace followed by 2 colons e.g. std::cout<<"hello";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜