VS 2005:Dispose方法保留 [英] VS 2005: Dispose method Reserved
问题描述
全部 -
众所周知,Dispose方法在C ++ 8中保留,预期的语法
是使用~MyClass()。
在1.1中,我们曾经有过以下的Dispose结构
[C#中为简洁起见的代码:-)]
class MyClass:IDisposable
{
public void Dispose(){Dispose(true); GC.Supressxxx(本); }
~Dispose(){Dispose(false); } //终结者
protected virtual void Dispose(bool disposing)
{
if(disposing){xxx} //如果我们处理ilustrates在终结器中
yyyy
}
}
使用System.Windows.Forms.Form有这样一个模型,我们将它用于我们的
可终结类,以在单个protected
覆盖Dispose(bool disposing)中实现所有清理,其中很好地拥有处理参数。
现在C ++ 8.0,不允许按名称使用任何函数Dispose和结构
必须用新名称更改(protected virtual" DisposeObject" ;对于
实例)
问题是 -
为什么编译器禁止重载Dispose [如Dispose(bool) ]
方法。我可以理解Dispose()的保留方法但是为什么
甚至会重载?
如果我们只是用新编译器重新编译我们的代码(之后)语法
更改),我们将打破我们的客户。客户将不得不更改他们的
代码以覆盖Dispose方法!
有什么建议吗? [使用VS 2005 Beta 2]
提前致谢。
Regardz
Grafix。
All -
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().
In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:-)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}
System.Windows.Forms.Form used to have such a model, and we used it for our
finalizable classes to implement all cleanups in the single "protected
override Dispose(bool disposing)", which nicely has the "disposing" parameter.
Now C++ 8.0, disallows having any function by name Dispose and the structure
has to be changed with a new name (protected virtual "DisposeObject" for
instance)
The question is -
Why does the compiler forbids even overloaded Dispose [like Dispose(bool)]
methods. I can understand the reserved method for Dispose() but why for
overloads even?
If we simply re-compile our code with the new compiler (after syntax
changes), it we will break our clients. Clients will have to change their
code for overriding the Dispose method!
Any suggestions? [Using VS 2005 Beta 2]
Thanks in advance.
Regardz
Grafix.
推荐答案
您好Grafix!
Hi Grafix!
众所周知,Dispose方法在C ++ 8中保留,预期的语法是
使用~MyClass()。
在1.1中,我们曾经有过以下的Dispose结构
[C#中为简洁起见的代码:-)]
类MyClass:IDisposable
{
public void Dispose(){Dispose(true); GC.Supressxxx(本); }
~Dispose(){Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing){xxx} //如果我们在终结者中,则处理ilustrate
yyyy
}
}
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().
In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:-)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}
请参阅:Visual C ++中的析构函数和终结器
http://msdn2.microsoft.com/en- us / library / ms177197
简而言之:
1.1 mapps中的Dispose-Pattern:
void处置(布尔处理){
if(处理){
~T();
}否则{
!T();
}
}
所以你只需要实现~T和!T而你有期望的行为...
-
问候
Jochen
我关于Win32和.NET的博客
http://blog.kalmbachnet.de/
See: Destructors and Finalizers in Visual C++
http://msdn2.microsoft.com/en-us/library/ms177197
In short:
The Dispose-Pattern in 1.1 mapps to:
void Dispose(bool disposing) {
if (disposing) {
~T();
} else {
!T();
}
}
So you just need to implement ~T and !T and you have the desired behavior...
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jochen -
如果使用VC 2005编译的程序集必须由C#程序集使用,那么
那么它们如何覆盖& Dispose(bool disposing)方法,becoz
方法Dispose(bool)是否被C ++禁止?
我的观点是,如果用C ++编写的(ref)类具有终结器和
a dispose方法(用于确定性破坏) ),派生类(在
另一种语言,如C#)想要消耗它,然后重写Dispose(bool
处理)显示我们是否在终结器中或者不是很好,对于派生类。
MyClass的C#客户端
类CSharpClass:MyClass(参见OrigPosting示例中的MyClass)
{
protected override void Dispose(bool disposing)
{
if(disposing)//知道我们是否正在处置或者不付出任何努力
{
}
base.Dispose(disposing);
}
现在C#类已经确定它是否在终结器中*没有*做任何努力,通过检查处理参数。 C ++ 2005通过保留Dispose(bool)来剥离这个
设施!
Regardz
Grafix。
Jochen Kalmbach [MVP]"写道:
Jochen -
If an assembly compiled with VC 2005 must be consumed by a C# assembly,
then how can they override the "Dispose(bool disposing)" method, becoz the
method "Dispose(bool)" is forbidden by C++?
My point is that if a (ref) class written in C++ , has both a finalizer and
a dispose method (for deterministic destruction), and a derived class (in
another language like C#) wanted to consume it, then overriding Dispose(bool
disposing) shows if we are in finalizer or not nicely, for the derived class.
C# client of MyClass
class CSharpClass : MyClass (see MyClass from OrigPosting sample)
{
protected override void Dispose(bool disposing)
{
if(disposing) // Got to know if we are disposing or not w.o any effort
{
}
base.Dispose(disposing);
}
Now the C# class has found out if it is in finalizer or not *without* doing
any effort, by examinin the "disposing" parameter. C++ 2005 strips this
facility by making Dispose(bool) reserved!
Regardz
Grafix.
"Jochen Kalmbach [MVP]" wrote:
Hi Grafix!
Hi Grafix!
众所周知,Dispose方法在C ++ 8中保留,预期的语法是
使用~MyClass()。
在1.1中,我们曾经有过以下的Dispose结构
[代码用C#说明为简洁:-)]
类MyClass:IDisposable
{
public void Dispose(){Dispose(true); GC.Supressxxx(本); }
~Dispose(){Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing){xxx} //如果我们在终结者中,则处理ilustrate
yyyy
}
}
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().
In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:-)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}
请参阅:Visual C ++中的析构函数和终结器
http://msdn2.microsoft.com/en-us/library/ms177197
简而言之:
1.1 mapps中的Dispose-Pattern:
void Dispose(bool disposing){
if(disposing){
〜 T();
} else {
!T();
}
所以你只需要实现~T和!T和你有所期望的行为......
-
Jochen
关于Win32和.NET的博客
http:/ /blog.kalmbachnet.de /
See: Destructors and Finalizers in Visual C++
http://msdn2.microsoft.com/en-us/library/ms177197
In short:
The Dispose-Pattern in 1.1 mapps to:
void Dispose(bool disposing) {
if (disposing) {
~T();
} else {
!T();
}
}
So you just need to implement ~T and !T and you have the desired behavior...
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
奇怪的是,您可以使用Dispose(Boolean ...。
由于某种原因,编译器保留Dispose(bool ...),但不是
Dispose(Bo olean ...),所以有你的解决办法。
-
David Anton
www.tangiblesoftwaresolutions.com
即时C#:VB转C#转换器
即时VB:C#到VB转换器
即时C ++:C#到C ++转换器& VB到C ++转换器
即时J#:VB到J#转换器
" Grafix"写道:
Curiously enough, you can use "Dispose(Boolean...".
For some reason the compiler reserves "Dispose(bool...)", but not
"Dispose(Boolean...)", so there''s your work-around.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
"Grafix" wrote:
全部 -
众所周知,Dispose方法在C ++ 8中保留,预期的语法是使用~MyClass()。
在1.1中,我们曾经有过以下的Dispose结构
[C#中为简洁起见的代码:-)]
类MyClass:IDisposable
{
公开void Dispose(){Dispose(true); GC.Supressxxx(本); }
~Dispose(){Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing){xxx} //如果我们在终结者中,则处理ilustrate
yyyy
曾经有过这样一个模型的System.Windows.Forms.Form,我们将它用于我们的可终结类来实现单个保护中的所有清理工作
覆盖Dispose(bool disposing)",它很好地具有处理功能。参数。
现在C ++ 8.0,不允许任何名称的功能Dispose和结构
必须用新名称更改(受保护的虚拟DisposeObject用于
实例)
问题是 -
为什么编译器禁止重载Dispose [如Dispose(bool)]
方法。我可以理解Dispose()的保留方法但是为什么
重载甚至?
如果我们只是用新编译器重新编译我们的代码(在语法更改之后),我们会打破我们的客户。客户将不得不更改他们的代码以覆盖Dispose方法!
任何建议? [使用VS 2005 Beta 2]
提前致谢。
Regardz
Grafix。
All -
As we all know, Dispose method is reserved in C++ 8 and the expected syntax
is to use ~MyClass().
In 1.1, we used to have following structure for Dispose
[Code illustrated in C# for brevity:-)]
class MyClass : IDisposable
{
public void Dispose() { Dispose(true); GC.Supressxxx(this); }
~Dispose() { Dispose(false); } // Finalizer
protected virtual void Dispose(bool disposing)
{
if(disposing) { xxx } // Disposing ilustrates if we are in finalizer
yyyy
}
}
System.Windows.Forms.Form used to have such a model, and we used it for our
finalizable classes to implement all cleanups in the single "protected
override Dispose(bool disposing)", which nicely has the "disposing" parameter.
Now C++ 8.0, disallows having any function by name Dispose and the structure
has to be changed with a new name (protected virtual "DisposeObject" for
instance)
The question is -
Why does the compiler forbids even overloaded Dispose [like Dispose(bool)]
methods. I can understand the reserved method for Dispose() but why for
overloads even?
If we simply re-compile our code with the new compiler (after syntax
changes), it we will break our clients. Clients will have to change their
code for overriding the Dispose method!
Any suggestions? [Using VS 2005 Beta 2]
Thanks in advance.
Regardz
Grafix.
这篇关于VS 2005:Dispose方法保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!