On Mon, 2002-09-09 at 22:15, Ricardo Kirkner wrote:
I am sorry to bother the list with this question, but I could not find a satisfying answer anywhere.I would guess that the `static' requirement is for language simplification -- there is only one (consistent) way for operators to be expressed. In C++, some operators must be global (read: operator<<, operator>>). C# doesn't have global scope, so class-static is the closest equivalent. Since some operators would need to be class-static, it was probably more consistent to just require that they all be class-static. That's my best guess, anyway. I have no idea why `public' is required. 2. If I want to overload the ++ operator in a class that I dont want to be public, but internal or private, how can I prevent someone from accessing a method (the ++ operator) that has been defined public?Question to ponder: how will they access the public method if they can't access the class in the first place? Consider methods that must be public, such as Object.ToString(). Is it a problem that this method is public, even in your internal classes? Not usually -- non-internal users can't access the class, so the existence of a public ToString() method shouldn't be a problem. The same should be true of any operators. (The answer to the above question of accessing internal classes is simple, actually: use Reflection and poke around... Alternatively, modify the runtime to permit poking on internal data. It's hard to protect against the runtime.... However, these ideas are not trivial, and can probably be ignored most of the time.) Another question is:I've seen two descriptions of how to handle the non-existence of the assignment operator. The first is to kludge it by having an `Assign' method: class Foo {The other kludge is to make the objects cloneable, and clone the object before making changes you want to preserve: class Bar : ICloneable {However, the real question remains: why do you need an assignment operator? Assignment operators are useful in C++ when wrapping resources, such as memory, files, thread locks, etc., to make sure that the resource is properly managed (in concert with the copy constructor and destructor)... In C#, the garbage collector is used for resource management, removing (what I've found) the greatest need for the trio of C++ copy constructor, assignment operator, and destructor. If you need something more deterministic, the IDisposable interface/idiom is appropriate. I would need to know more about your program to have any idea why you would need a copy constructor or assignment operator. With regards, and hoping someone can give me a hintHope some of this helped. - Jon |
- [Mono-list] Question about C# Ricardo Kirkner
- Re: [Mono-list] Question about C# Jonathan Pryor
- Re: [Mono-list] Question about C# Ricardo Kirkner
- Re: [Mono-list] Question about C# BioChem333
- Re: [Mono-list] Question about C# Jonathan Pryor
- Re: [Mono-list] Question about C# Miguel de Icaza
