开发者

The standard Namespace problem

Hii ,

I am relatively new to programming C++ . I do know that the functions cout , cin etc... are defined in the standard name space . But we also include iostream header file for running the program .

So , is it like

 namespace std
   {

     declaration of cout 

     declaration of c开发者_JAVA技巧in 

     ..... some other declarations etc....

   }

and theior actual implementations inside istream and ostream ... ????

Or , is it the other way round ...??? like ....

namespace std 
   {
     complete definition of cout 
     complete definition of cin  
     .........

   } 

and their signatures are just placed in the iostream file like ...

iostream file 
  {
    std :: cout 
    std :: cin 
    .....

  }

Please provide any examples or links that you might think will help me understand better


I do know that the functions cout , cin etc... are defined in the standard name space.

These are not really functions, but global instances of basic_ostream and basic_istream.

But we also include iostream header file for running the program.

You rather include headers so you can compile your source (the compiler needs to declarations etc).

The rest of the question is rather fuzzy. How the standard library is implemented is pretty much up to the implementation. The standard requires that if you include iostream, you will get the declarations of the following globals:

namespace std {
  extern istream cin;
  extern ostream cout;
  extern ostream cerr;
  extern ostream clog;

  extern wistream wcin;
  extern wostream wcout;
  extern wostream wcerr;
  extern wostream wclog;
}


The standard really doesn't say. It's entirely possible for the implementer to do it as a header-only library, but it's much more likely for them to just put the declarations in headers and put the implementations in the CRT.

EDIT: However, the definitions for cin, cout, etc need to be extern and defined in some sort of library. (See UncleBens' answer)


cin and cout are not simple variables - with cerr they are streams registered by default for every application using iostream. you can't use them without including that header.


To use cin and cout you really only have to know that they're in the std namespace and that you need to include iostream to use them.

To give you an idea how it might be implemented, the definition of class std::ostream could be in the header ostream, which is included by iostream. Also in the header ostream, std::cout could be defined as a reference to an std::ostream.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜