Nếu tôi biên dịch và chạy như sau:Tại sao không bảo vệ C++ - Cli destructors gây ra lỗi biên dịch?
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;
}
... mã biên dịch mà không có lỗi và chạy cho tôi này:
Creating C1
Destroying C1
Press any key to continue . . .
Nếu tôi làm như vậy trong C++ Tôi nhận được một lỗi dọc theo các dòng sau:
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'
... vậy tại sao nó hợp lệ trong CLI?