在上次介绍显示释放资源的文章,我提到了C++/CLI中提供了一种新的对象实例化语法,就仿佛在native c++中在静态栈上定义一个对象一样。今天就让我们来考察一下这个特性。另外,因为在上个帖子里回答Ninputer的一个问题时,犯了一个错误,我在这里纠正一下...
定义以下以一个类。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
然后我们定义一个函数来使用这个类:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
我们来看一下生成的IL代码
.method assembly static void foo() cil managed
{
// Code size 29 (0x1d)
.maxstack 1
.locals init ([0] class Test V_0)
IL_0000: ldstr "Hello"
IL_0005: newobj instance void Test::.ctor(string)
IL_000a: stloc.0
IL_000b: ldloc.0
IL_000c: call instance void Test::Hello()
IL_0011: ldloc.0
IL_0012: call instance string Test::get_Message()
IL_0017: call void [mscorlib]System.Console::WriteLine(string)
IL_001c: ret
} // end of method 'Global Functions'::foo
如果我们把上面的foo()函数改成gcnew的形式:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
然后观察生成的IL代码:
.method assembly static void foo() cil managed
{
// Code size 29 (0x1d)
.maxstack 1
.locals init ([0] class Test V_0)
IL_0000: ldstr "Hello"
IL_0005: newobj instance void Test::.ctor(string)
IL_000a: stloc.0
IL_000b: ldloc.0
IL_000c: call instance void Test::Hello()
IL_0011: ldloc.0
IL_0012: call instance string Test::get_Message()
IL_0017: call void [mscorlib]System.Console::WriteLine(string)
IL_001c: ret
} // end of method 'Global Functions'::foo
可以看到生成的IL代码与前面的是完全一致的。由此可见,这种新的语法只是编译器提供的一种机制,并不是真的在Stack上实例化对象。那么为什么要提供这种语法呢?或者说两种的区别在哪里呢?那就是在上一篇文章中所提到的显式释放资源。只有用Stack方法来定义变量,才能获得自动调用Dispose()方法的好处。
或许,我们今后应该尽量少用gcnew,除非你想手动控制Dispose()方法调用的时机。唯一比较遗憾的是,由于是从vc 2005 tool refreshes才开始支持这种语法,所以vc express的IDE中的intellisense功能还没有跟上,使用“点”操作符时,没有任何提示,不过相信在beta2里一定会全面支持的。另外MS的员工还透露说beta2中,STL.NET可能就会现身了,真是非常期待。
另:
昨天Ninputer问我Stack定义的对象,是用->访问成员还是用“点”来访问成员。我回答是用->来访问,其实应该是“点”才对。前几天试验的时候不知道为什么编译器没有报错......可惜代码已经不在了,也没法知道当时犯了什么错 今天写这个帖的时候才发现不对了...