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++ 標準ライブラリ