方法: interface 静的コンストラクターを定義する (C++/CLI)

インターフェイスでは、静的コンストラクターを定義して、静的データ メンバーの初期化に使用できます。 静的コンストラクターは、多くても 1 回だけ、静的インターフェイス メンバーが初めてアクセスされる前に呼び出されます。

// mcppv2_interface_class2.cpp
// compile with: /clr
using namespace System;

interface struct MyInterface {
   static int i;
   static void Test() {
      Console::WriteLine(i);
   }

   static MyInterface() {
      Console::WriteLine("in MyInterface static constructor");
      i = 99;
   }
};

ref class MyClass : public MyInterface {};

int main() {
   MyInterface::Test();
   MyClass::MyInterface::Test();

   MyInterface ^ mi = gcnew MyClass;
   mi->Test();
}
in MyInterface static constructor
99
99
99

関連項目

interface クラス