/***************************************************************************** *** FILE: managed_op_ref.cpp *** DATE: 04/10/2004 *** AUTHOR: Daniel Dementiev *** GOAL: This example show how to overload some operators for reference *** managed type. *** To build the project run: *** ...> cl managed_op_ref.cpp /clr *****************************************************************************/ #using using namespace System; __gc class Complex { private: double x, y; public: Complex(double X, double Y) : x(X), y(Y) {}; void print() { Console::WriteLine(S"{0}+{1}*i", x.ToString(), y.ToString()); }; static Complex& op_Addition(Complex& l, Complex& r) { Complex *res = new Complex(l.x+r.x, l.y+r.y); return *res; }; static Complex& op_Subtraction(Complex& l, Complex& r) { Complex *res = new Complex(l.x-r.x, l.y-r.y); return *res; }; static Complex& op_UnaryNegation(Complex& r) { Complex *res = new Complex(-r.x, -r.y); return *res; }; static Complex& op_Multiply(Complex& l, double r) { Complex *res = new Complex(r*l.x, r*l.y); return *res; }; static Complex& op_Multiply(double r, Complex& l) { Complex *res = new Complex(r*l.x, r*l.y); return *res; }; static Complex& op_Assign(Complex &l, Complex &r) { l.x = r.x; l.y = r.y; return l; }; }; void main() { Complex &a = *new Complex(1, 3); Complex &b = *new Complex(2, 1); Complex &c = *new Complex(0, 0); c = (a + b) * 2; c.print(); c = 3 * c; c.print(); }