문제

내 컴퓨터 공학 교수님은 우리가 다음의 선언문을 찾아보길 원하십니다. 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 수업?

편집하다:

무엇입니까? wcout 거기 선언?내가 정확하게 기억한다면 문자 "w"는 "wide"를 의미하지만 그것이 어떤 의미를 갖는지는 모르겠습니다.은 무엇입니까? wcout 그리고 wostream?

도움이 되었습니까?

해결책

예, 그것은 실제로 다음의 선언입니다. 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;
}

p1 헤더 <iostream> 선언된 함수에 의해 제공되는 표준 C 스트림과 객체를 연관시키는 객체를 선언합니다. <cstdio> (27.9.2), 이러한 객체를 사용하는 데 필요한 모든 헤더를 포함합니다.

다른 팁

이것이 cout의 선언이고 cout은 ostream 클래스의 인스턴스입니까?

응, 선언이야. std::cout 그리고 네, 그것은 다음의 예입니다 std::ostream.선언된다 extern 헤더가 여러 번역 단위에 포함되어 있어도 객체는 한 번만 생성됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top