Compartir vía


Cómo: Definir un constructor estático de interfaz (C++/CLI)

Una interfaz puede tener un constructor estático, que se puede usar para inicializar miembros de datos estáticos. A un constructor estático se le llama como máximo una vez y será antes de que se acceda a un miembro de interfaz estática por primera vez.

Ejemplo

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

Consulte también

clase de interfaz