Delen via


Compilerfout C3894

'var': l-value gebruik van initonly statisch datalid is alleen toegestaan in de klasseconstructor van klasse 'class'

Opmerkingen

Statische initonly gegevensleden kunnen alleen worden gebruikt als l-values op hun declaratiepunt of in een statische constructor.

Niet-statische gegevensleden kunnen alleen worden gebruikt als l-waarden op hun declaratiepunt of in instantieconstructors (niet-statische) constructors.

Example

In het volgende voorbeeld wordt C3894 gegenereerd:

// C3894.cpp
// compile with: /clr
ref struct Y1 {
   initonly static int data_var = 0;

public:
   // class constructor
   static Y1() {
      data_var = 99;   // OK
      System::Console::WriteLine("in static constructor");
   }

   // not the class constructor
   Y1(int i) {
      data_var = i;   // C3894
   }

   static void Test() {}

};

int main() {
   Y1::data_var = 88;   // C3894
   int i = Y1::data_var;
   Y1 ^ MyY1 = gcnew Y1(99);
   Y1::Test();
}