宣布cout在哪里?

Mos*_*she 9 c++ terminology cout ostream

我的计算机科学教授希望我们找到宣言cout.我使用g ++和-E参数编译了一个简单的Hello world程序.这是我的hello.cpp的样子:

#include <iostream>

using namespace std;

int main(){

  string name="";

  cout << "Good morning! What's your name?";

  cin >> name;

  cout << "Hello " << name << ".\n";

  return 0; 

}
Run Code Online (Sandbox Code Playgroud)

我的编译命令:

g++ -E hello.cpp > hello.p
Run Code Online (Sandbox Code Playgroud)

在hello.p中,我在VIM中运行了一个搜索,如下所示:

:/cout
Run Code Online (Sandbox Code Playgroud)

我看到以下行:

extern ostream cout;
Run Code Online (Sandbox Code Playgroud)

那是声明cout,并且是班级的cout一个实例ostream吗?

编辑:

wcout那里的宣言是什么?如果我没记错的话,字母"w"代表"宽",但我不知道它有什么含义.什么是a wcout和a wostream

Xeo*_*Xeo 8

是的,这确实是std::cout<iostream>标题内找到的声明.

相关标准部分可在以下位置找到§27.4.1 [iostream.objects.overview]:

标题<iostream>简介

#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>

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;
}
Run Code Online (Sandbox Code Playgroud)

p1标头<iostream>声明了将对象与<cstdio>(27.9.2)中声明的函数提供的标准C流相关联的对象,并包括使用这些对象所需的所有标头.