I discovered the source of a really insidious bug today. It's all safely fixed now, but I'd like to understand why when I execute the following code:
using namespace System;
ref class EvilClass
{
public:
EvilClass()
{
}
void Print()
{
static bool enablePrint = false;
if( enablePrint )
{
Console::WriteLine("PrintEnabled");
}
else
{
Console::WriteLine("PrintDisabled");
}
enablePrint = true;
}
};
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
EvilClass^ ec = gcnew EvilClass();
ec->Print();
ec->Print();
delete ec;
ec = nullptr;
ec = gcnew EvilClass();
ec->Print();
ec->Print();
delete ec;
ec = nullptr;
return 0;
}
...I get the following:
Hello World
PrintDisabled
PrintEnabled
PrintEnabled
PrintEnabled
I had always assumed that the static would only persist between calls to the same instance of a class?