ฟังก์ชันการพิมพ์ใน cpp [ซ้ำกัน]

Oct 25 2020

ทำไมทุกครั้งที่ฉันพิมพ์ฟังก์ชันใน cpp มันจะพิมพ์ 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
}

ฉันแค่สงสัยว่าทำไมถึงพิมพ์ 1.

ขอบคุณสำหรับความช่วยเหลือของคุณ

คำตอบ

1 MikeCAT Oct 25 2020 at 20:20

ดูเหมือนว่ามันจะถูกตัดสินถ้าฟังก์ชั่น (แปลงตัวชี้ไปยังฟังก์ชั่น) จะไม่เป็นโมฆะกลับtrueและพิมพ์trueเป็น 1 นี้ได้รับการยืนยันโดยการเพิ่มและเห็นการส่งออกที่มีการเปลี่ยนแปลงไป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
}