Druckfunktionen in cpp [Duplikat]

Oct 25 2020

Warum druckt es jedes Mal, wenn ich eine Funktion in cpp drucke, 1. dh

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

Ich frage mich nur, warum es 1 druckt.

Danke für Ihre Hilfe

Antworten

1 MikeCAT Oct 25 2020 at 20:20

Es scheint zu beurteilen, ob die Funktionen (in Zeiger auf Funktionen konvertiert) nicht null sind, zurückkehren trueund trueals 1 drucken . Dies kann durch Hinzufügen cout << std::boolalpha;und Anzeigen der Ausgabe bestätigt werden 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
}