Funkcje drukowania w CPP [duplikat]

Oct 25 2020

Dlaczego za każdym razem, gdy drukuję funkcję w cpp, drukuje ona 1. ie

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

Zastanawiam się tylko, dlaczego drukuje 1.

Dzięki za pomoc

Odpowiedzi

1 MikeCAT Oct 25 2020 at 20:20

Wygląda na to, że ocenia, czy funkcje (konwertowane na wskaźniki do funkcji) nie są zerowe, zwracają truei wyświetlają truejako 1. Można to potwierdzić, dodając cout << std::boolalpha;i obserwując zmianę wyniku na 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
}