방법: 인터페이스 정적 생성자 정의(C++/CLI)

인터페이스에는 정적 데이터 멤버를 초기화하는 데 사용할 수 있는 정적 생성자가 있을 수 있습니다. 정적 생성자는 최대 한 번 호출되며 정적 인터페이스 멤버에 처음 액세스하기 전에 호출됩니다.

예시

// 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

참고 항목

인터페이스 클래스