【发布时间】:2015-04-30 16:40:55
【问题描述】:
如果我编译并运行以下内容:
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;
}
...代码编译没有错误并运行给我这个:
Creating C1
Destroying C1
Press any key to continue . . .
如果我在 C++ 中执行相同操作,则会收到以下错误:
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'
...那为什么它在 CLI 中有效?
【问题讨论】:
标签: c++-cli destructor