locked
Passing a structure by reference

    Question

  • Hi, how to pass a value struct by reference like in C# or C++/CLI ?

    public value struct Matrix
    {

    public :

        float M11;
        float M12;
        float M21;
        float M22;
        float M31;
        float M32;
    };

    public ref class Image sealed
    {

    public : // ABI.

        // error C3699: '%' : cannot use this indirection on type 'Matrix'.
        void Transform( Matrix% m ) { }
    };


    • Edited by t-n-x Monday, March 5, 2012 11:22 PM
    Monday, March 5, 2012 11:21 PM

Answers

  • There isn't a direct correspondence to the CLI tracking reference. It's a garbage collected object, while the C++/CX public value struct is a data blob.

    This is the by-val way with the data just put on the stack:

    void Transform(Matrix2 m) 
    {
    	mytype f1,f2,f3;
    	f1 = m.M1;
    	f2 = m.M2;
    	f3 = m.M3;
    }

    If you want to pass byref, use a ptr:

    void Transform2(Matrix2* m) 
    {
    	float f1,f2,f3;
    	f1 = m->M1;
    	f2 = m->M2;
    	f3 = m->M3;
    }
    -Steve

    Wednesday, March 7, 2012 7:03 PM
    Moderator

All replies

  • Hi

    Update: In WinRT, structs are value type, we can't pass it as reference type.

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Yi Feng Li Thursday, March 8, 2012 2:44 AM Updated
    Wednesday, March 7, 2012 11:57 AM
  • There isn't a direct correspondence to the CLI tracking reference. It's a garbage collected object, while the C++/CX public value struct is a data blob.

    This is the by-val way with the data just put on the stack:

    void Transform(Matrix2 m) 
    {
    	mytype f1,f2,f3;
    	f1 = m.M1;
    	f2 = m.M2;
    	f3 = m.M3;
    }

    If you want to pass byref, use a ptr:

    void Transform2(Matrix2* m) 
    {
    	float f1,f2,f3;
    	f1 = m->M1;
    	f2 = m->M2;
    	f3 = m->M3;
    }
    -Steve

    Wednesday, March 7, 2012 7:03 PM
    Moderator