共用方式為


編譯器錯誤 C3894

'var' :只允許在類別 'class' 的類別建構函式中使用 l-value 使用 initonly static 數據成員

靜態 initonly 數據成員只能在宣告點或靜態建構函式中當做 l 值使用。

實例 (非靜態) 非靜態數據成員只能在宣告點或實例 (非靜態) 建構函式中當做 l 值使用。

下列範例會產生 C3894:

// 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();
}