Edit

Share via


Compiler Error C3893

'var' : l-value use of initonly data member is only allowed in an instance constructor of class 'type_name'

Static initonly data members can only have their address taken in a static constructor.

Instance (non-static) initonly data members can only have their address taken in instance (non-static) constructors.

The following sample generates C3893:

// C3893.cpp
// compile with: /clr
ref struct Y1 {
   Y1() : data_var(0) {
      int% i = data_var;   // OK
   }
   initonly int data_var;
};

int main(){
   Y1^ y= gcnew Y1;
   int% i = y->data_var;   // C3893
}