在 C/C++ 中,__PRETTY_FUNCTION__、__FUNCTION__、__func__ 有什麼區別?


在本文中,我們將詳細介紹 C++ 中的 __FUNCTION__、__func__ 和 __PRETTY_FUNCTION__ 之間有什麼區別。

基本上,__FUNCTION__ 和 __func__ 是相同的。某些較舊版本的 C 和 C++ 支援 __func__。此宏用於獲取當前函式的名稱。__PRETTY_FUNCTION__ 用於返回有關該函式的詳細資訊。使用這個,我們可以瞭解使用哪個函式,它屬於哪個類,等等。

例如

#include<iostream>
using namespace std;
class MyClass{
   public:
      void Class_Function(){
         cout << "The result of __PRETTY_FUNCTION__: " << __PRETTY_FUNCTION__ << endl;
      }
};
void TestFunction(){
   cout << "Output of __func__ is: " << __func__ << endl;
}
main() {
   cout << "Output of __FUNCTION__ is: " << __FUNCTION__ << endl;
   TestFunction();
   MyClass myObj;
   myObj.Class_Function();
}

輸出

Output of __FUNCTION__ is: main
Output of __func__ is: TestFunction
The result of __PRETTY_FUNCTION__: void MyClass::Class_Function()

更新於:2019 年 7 月 30 日

1K+ 瀏覽

啟動你的職業生涯

透過完成此課程獲得認證

開始
廣告