Thanks for your reply, Tom.
I'm familiar how destructors and finalizers map to the .NET Dispose pattern.
What I'm attempting to illustrate is that the compiler does not prevent me
from putting a destructor and finalizer under a private block, which mislead
me into thinking that I could limit deterministic destruction by limiting
access to the destructor. I feel that if the compiler allows me to do it,
then it should enforce visibility rules regardless of what IL it produces in
the back end.
Having said that, I just took a look at the C++/CLI specification to see if
it says anything on the matter. Under section 19.13.1 reads: The
access-specifier of a destructor in a ref class is ignored. I guess that
answers that. :) I would suggest that the compiler present an error (or
possibly a warning) when a destructor is defined under a non-public access
modifier in a ref class. C# enforces this by not allowing you to change the
access level of an interface method:
class A
: IDisposable
{
private void IDisposable.Dispose() { } // error CS0106: The modifier
'private'
// is
not valid for this item
// or
private virtual void Dispose() { } // error CS0621: virtual or
abstract
// members
cannot be private
// or
protected virtual void Dispose() { } // error CS0536: 'B' does not
implement
// member
System.IDisposable.Dispose().
//
'B.Dispose()' is either static, not public,
// or has a
wrong return type.
}
As for the solution you presented regarding limiting the deterministic
destruction of an object... I would agree with you that if I wanted to
prevent EVERYONE from calling Dispose() on my object, I should just not
define the destructor. However, I'm looking to just RESTRICT who can do it
to enforce encapsulation without having to implement a wrapper class, perform
an expensive copy, or rely on documentation to enforce a policy.
> > So, given that access modifiers don't have any effect on the visibility on
> > destructors in ref classes, how would one prevent a client from
[quoted text clipped - 35 lines]
>
> Tom
Tamas Demjen - 13 Jun 2006 19:49 GMT
> I would agree with you that if I wanted to
> prevent EVERYONE from calling Dispose() on my object, I should just not
> define the destructor.
Having a strong C++ background, I understand your problem, but C++/CLI
is not exactly C++. It has to follow the rules of the CLI closely. I'm
not an authority, but I think the .NET framework insists that Dispose be
public, so you can not restrict it. Even if you could do that, that
restriction would be half baked, as Dispose would still be publicly
available via IDisposable.
I agree that the compiler should show at least a warning message
pointing out that destructors for ref classes must be public.
> However, I'm looking to just RESTRICT who can do it
> to enforce encapsulation without having to implement a wrapper class, perform
> an expensive copy, or rely on documentation to enforce a policy.
As a workaround, you could create your own interface,
IMyRestrictedDispose, where Dispose is defined as protected, for
example. The compiler is just not going to associate that with the
concept of destructors. Deterministic destruction with the stack syntax
is only possible if the destructor is public. You can, however, still
use the RAII concept with a helper class to call your restricted cleanup
in a deterministic way:
ref class Guard
{
public:
Guard(A^ source) : a(source) { }
~Guard() { a->MyRestrictedCleanup(); }
private:
A^ a;
};
Tom