在 ABI 界限的可攜性

在二進位介面界限使用足夠的可攜式類型和慣例。 「可攜式類型」是 C 內建類型或結構,只包含 C 內建類型。 只有在呼叫端和被呼叫者同意配置、通話慣例等時,才能使用類別類型。只有當兩者都使用相同的編譯器和編譯器設定進行編譯時,才有可能。

如何簡化 C 可攜性的類別

當呼叫端可以使用另一個編譯器/語言編譯時,請使用特定的呼叫慣例,將 「扁平化」到 extern 「C」 API:

// class widget {
//   widget();
//   ~widget();
//   double method( int, gadget& );
// };
extern "C" {        // functions using explicit "this"
   struct widget;   // opaque type (forward declaration only)
   widget* STDCALL widget_create();      // constructor creates new "this"
   void STDCALL widget_destroy(widget*); // destructor consumes "this"
   double STDCALL widget_method(widget*, int, gadget*); // method uses "this"
}

另請參閱

歡迎回到 C++
C++ 語言參考
C++ 標準程式庫