cpp의 인쇄 기능 [중복]

Oct 25 2020

cpp에서 함수를 인쇄 할 때마다 1이 인쇄되는 이유는 무엇입니까?

#include <iostream>
using namespace std;
void f1(){
   cout<<"called function f";
}
int f2(){
   cout<<"called f2";
   return 1;
}
int main(){
   cout << f2 <<endl; // prints 1
   cout << f1 <<endl; // still prints 1
}

왜 1이 인쇄되는지 궁금합니다.

당신의 도움을 주셔서 감사합니다

답변

1 MikeCAT Oct 25 2020 at 20:20

함수 (함수에 대한 포인터로 변환 된 함수)가 null이 아닌지 판단하고 반환 true하고 1로 인쇄하는 것 true같습니다. 이는 cout << std::boolalpha;출력이로 변경된 것을 추가 하고 확인하여 확인할 수 있습니다 true.

#include <iostream>
using namespace std;
void f1(){
   cout<<"called function f";
}
int f2(){
   cout<<"called f2";
   return 1;
}
int main(){
   cout << std::boolalpha;
   cout << f2 <<endl; // prints true
   cout << f1 <<endl; // also prints true
}