G
Guest
This compiles and runs successfully in VS2005:
ref class A
{
private:
~A() { this->!A(); } // private!
!A() { } // private!
};
....
A^ a1 = gcnew A();
a1->~A(); // I would expect this to signal a compile
// error regarding accessibility
delete a1; // As well as this.
A a2; // As well as this.
a2.~A(); // And as well as this.
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
deterministically cleaning up a managed reference object?
Another thing I find peculiar is that you can call the destructor on an
interface class, even though the compiler enforces the fact that an interface
class cannot have a destructor.
interface class B { };
ref class C : B { };
....
B^ b = gcnew C();
b->~B(); // Compiles fine???
delete b; // this too?
I have a situation where I want to prevent clients from deterministic
destruction of certain objects. For example, let's say I return a special
"read-only" interface to an internal object. I shouldn't be able to destroy
the object via the interface (by calling "delete" or explicitly calling the
destructor as demonstrated above) as this would break encapsulation. I also
don't want to return a copy of the internal object as it might incur a major
performance penalty. I would enforce this in standard C++ by returning a
reference to a const object. How would one recommend I do it in CLI?
ref class A
{
private:
~A() { this->!A(); } // private!
!A() { } // private!
};
....
A^ a1 = gcnew A();
a1->~A(); // I would expect this to signal a compile
// error regarding accessibility
delete a1; // As well as this.
A a2; // As well as this.
a2.~A(); // And as well as this.
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
deterministically cleaning up a managed reference object?
Another thing I find peculiar is that you can call the destructor on an
interface class, even though the compiler enforces the fact that an interface
class cannot have a destructor.
interface class B { };
ref class C : B { };
....
B^ b = gcnew C();
b->~B(); // Compiles fine???
delete b; // this too?
I have a situation where I want to prevent clients from deterministic
destruction of certain objects. For example, let's say I return a special
"read-only" interface to an internal object. I shouldn't be able to destroy
the object via the interface (by calling "delete" or explicitly calling the
destructor as demonstrated above) as this would break encapsulation. I also
don't want to return a copy of the internal object as it might incur a major
performance penalty. I would enforce this in standard C++ by returning a
reference to a const object. How would one recommend I do it in CLI?