Compiler Error C3162

 

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 C3162.

type' : a reference type which has a destructor cannot be used as the type of static data member 'member'

The common language runtime cannot know when to run a user-defined destructor when the class also contains static member function.

A destructor will never be run unless the object is deleted explicitly.

For more information, see,

The following sample generates C3162.

// C3162.cpp  
// compile with: /clr /c  
ref struct A {  
   ~A() { System::Console::WriteLine("in destructor"); }  
   static A i;   // C3162  
   static A^ a = gcnew A;   // OK  
};  
  
int main() {  
   A ^ a = gcnew A;  
   delete a;  
}  

Show: