質問

私のコンピュータサイエンス教授は、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; 

}
.

私のコンパイルコマンド:

g++ -E hello.cpp > hello.p
.

hello.pでは、私はそのようにVimで検索しました:

:/cout
.

次の行が見えます:

extern ostream cout;
.

coutの宣言、およびcoutクラスのインスタンスである。

編集:

そこにostream宣言は何ですか?私が正しく思い出されたら「w」は「幅」を表していますが、それがある意味を知りません。wcoutwcoutとは何ですか?

役に立ちましたか?

解決

Yes, that is indeed the declaration of std::cout, found inside the <iostream> header.

The relevant standard part can be found in §27.4.1 [iostream.objects.overview]:

Header <iostream> synopsis

#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;
}

p1 The header <iostream> declares objects that associate objects with the standard C streams provided for by the functions declared in <cstdio> (27.9.2), and includes all the headers necessary to use these objects.

他のヒント

Is that the declaration of cout, and is cout an instance of the ostream class?

Yes, that is the declaration of std::cout and yes it's an instance of std::ostream. It is declared extern so that the object is only created once even if the header is included in multiple translation units.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top