diff --git a/Examples/test-suite/csharp/operator_overload_runme.cs b/Examples/test-suite/csharp/operator_overload_runme.cs index 711301a3b..294bb7e77 100644 --- a/Examples/test-suite/csharp/operator_overload_runme.cs +++ b/Examples/test-suite/csharp/operator_overload_runme.cs @@ -31,7 +31,16 @@ public class runme if (op.i != 99) throw new Exception("operator-- prefix failed (op)"); if (opNew.i != 99) throw new Exception("operator-- prefix failed (opNew)"); } + + // overloaded operator class + Op k = new OpDerived(3); + int check_k = k.IntCast(); + Assert(check_k == 6); + } + + public static void Assert(bool b) { + if (!b) + throw new Exception("Assertion failed"); } } - diff --git a/Examples/test-suite/java/operator_overload_runme.java b/Examples/test-suite/java/operator_overload_runme.java index 491146124..8a9dddf6b 100644 --- a/Examples/test-suite/java/operator_overload_runme.java +++ b/Examples/test-suite/java/operator_overload_runme.java @@ -94,6 +94,16 @@ public class operator_overload_runme { Op op = j.MinusMinusPostfix(0); Assert(j.getI() == op.getI()-1); } + + // cast operators + Op k = new Op(3); + int check_k = k.IntCast(); + Assert(check_k == 3); + + Op l = new Op(4); + double check_l = l.DoubleCast(); + Assert(check_l == 4); + } public static void Assert(boolean b) { diff --git a/Examples/test-suite/operator_overload.i b/Examples/test-suite/operator_overload.i index 7bb20d127..92eb9669c 100644 --- a/Examples/test-suite/operator_overload.i +++ b/Examples/test-suite/operator_overload.i @@ -71,6 +71,9 @@ see bottom for a set of possible tests %rename(OrOperator) operator ||; #endif +%rename(IntCast) operator int(); +%rename(DoubleCast) operator double(); + %inline %{ #if defined(_MSC_VER) @@ -79,12 +82,14 @@ see bottom for a set of possible tests #include -class Op{ +class Op { public: int i; - Op(int a=0):i(a) + Op(int a=0) : i(a) {} - Op(const Op& o):i(o.i) + Op(const Op& o) : i(o.i) + {} + virtual ~Op() {} friend Op operator &&(const Op& a,const Op& b){return Op(a.i&&b.i);} @@ -134,6 +139,10 @@ public: // TODO: <<,<<= + // cast operators + operator double() { return i; } + virtual operator int() { return i; } + // This method just checks that the operators are implemented correctly static void sanity_check(); }; @@ -211,6 +220,17 @@ __concat__ for contatenation (if language supports) */ +%inline %{ +class OpDerived : public Op { +public: + OpDerived(int a=0) : Op(a) + {} + + // overloaded + virtual operator int() { return i*2; } +}; +%} + %{ @@ -314,6 +334,15 @@ void Op::sanity_check() assert(j.i == original); assert(newOp.i == newInt); } + + // cast operators + Op k=3; + int check_k = k; + assert (check_k == 3); + + Op l=4; + double check_l = l; + assert (check_l == 4); } %}