locked
operator overloading and extension method for a C++ component

    Question

  • I have created a public C++ value class (shared with C#):

    public value struct float2
    {
    	float x,y;
    };
    float2 operator+(float2& a, float2& b)
    {
    	float2 r;
    	r.x = a.x + b.x;
    	r.y = a.y + b.y;
    	return r;
    }
    float length(float2& a) { return sqrt(a.x*a.x+a.y*a.y); }

    and I have a '+' operator that I can use in c++ and a length function. Unfortunately they are not available in C#!

    So I have to redefine method in C# to do just that...

    I wonder if there is a more elegant way?

    Sunday, December 16, 2012 4:48 PM

Answers

  • Hi,

    I think you should achieve the simple operator function in C#.

        public struct Complex
        {
            public int real;
            public int imaginary;
    
            public Complex(int real, int imaginary)
            {
                this.real = real;
                this.imaginary = imaginary;
            }
            public static Complex operator +(Complex c1, Complex c2)
            {
                return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
            }
    
        }
    
    
                Complex num1 = new Complex(2, 3);
                Complex num2 = new Complex(3, 4);
    
                //Add two Complex objects (num1 and num2) through the overloaded plus operator:
                Complex sum_Add = num1 + num2;

    WRL is based on COM, I don't think we can use operator overload in COM. So that we need a named function for operator.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lloyd Tuesday, December 18, 2012 9:56 AM
    Tuesday, December 18, 2012 6:03 AM

All replies

  • Hi,

    I think you should achieve the simple operator function in C#.

        public struct Complex
        {
            public int real;
            public int imaginary;
    
            public Complex(int real, int imaginary)
            {
                this.real = real;
                this.imaginary = imaginary;
            }
            public static Complex operator +(Complex c1, Complex c2)
            {
                return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
            }
    
        }
    
    
                Complex num1 = new Complex(2, 3);
                Complex num2 = new Complex(3, 4);
    
                //Add two Complex objects (num1 and num2) through the overloaded plus operator:
                Complex sum_Add = num1 + num2;

    WRL is based on COM, I don't think we can use operator overload in COM. So that we need a named function for operator.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lloyd Tuesday, December 18, 2012 9:56 AM
    Tuesday, December 18, 2012 6:03 AM
  • What i thought, can't do that in my C++ component.. :/

    That's ok, I already implemented some interesting C# extension methods! 

    Tuesday, December 18, 2012 9:56 AM