From: Olly B. <ol...@su...> - 2006-01-02 01:45:30
|
In C#, the default == test for objects just tests if two object reference refer to the same object instance. That's useless if you're wrapping C++ iterator classes (since the equality test with the end iterator will always be false and any loops will never end!) So I want to overload operator== in C#, via SWIG. I'm using Mono C# 1.1.8.0, should that matter. I've attached simpleitor.i which I've been using to try to get this to work. This wraps a C++ class "I" which represents an iterator over the integers modulo 5. So i == i + 5 (except I've not implemented operator+). As well as operator==, I've overloaded operator!= (because the compiler insists), and also Equals and GetHashCode (I get warnings if I don't). I realise my GetHashCode implementation is rather primitive... I'm testing with Test.cs (also attached). But I get a null dereference if I run the example. I've no idea how to run a debugger on C#, but it appears that the problem is that SWIG generated code implicitly called from my operator== tests "obj == null" where obj is an instance of class I, so my overloaded == is being invoked. If I change this to "(object)obj == null", then I avoid the recursion and my test works. I'm not completely sure I've diagnosed the problem correctly, but the patch does seem to fix it. Thoughts? Cheers, Olly |