Se compilare ed eseguire il seguente:Perché non protetto C++ - I distruttori Cli causano errori di compilazione?
using namespace System;
ref class C1
{
public:
C1()
{
Console::WriteLine(L"Creating C1");
}
protected:
~C1()
{
Console::WriteLine(L"Destroying C1");
}
};
int main(array<System::String ^> ^args)
{
C1^ c1 = gcnew C1();
delete c1;
return 0;
}
... il codice viene compilato senza un errore e mi corre dare questo:
Creating C1
Destroying C1
Press any key to continue . . .
Se faccio lo stesso in C++ si ottiene un errore lungo le righe seguenti:
1>ProtectedDestructor.cpp(45): error C2248: 'C1::~C1' : cannot access protected member declared in class 'C1'
1> ProtectedDestructor.cpp(35) : compiler has generated 'C1::~C1' here
1> ProtectedDestructor.cpp(23) : see declaration of 'C1'
... quindi perché è valido in CLI?