Compiler Error C3893
Visual Studio 2015
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at 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
}
Show: