locked
constructor in public value struct

    Question

  • I have a C++/Cx class like so

    public value struct float2
    {
    	float x,y;
    };
    

    I'd like to add a constructor with parameter like 

    public value struct float2
    {
    	float x,y;
    	float2(float _x, float _y);
    };
    

    and I got a compiler error. Some googling later it seems it's not possible to add a constructor to a struct.. mm... fine I guess...

    Then later I was looking at 

    public value class Windows::Foundation::Size,

    And it has a parameterized constructor!!!

    Windows::Foundation::Size::Size(float w, float h)!!!


    This WinRT API value class has a public constructor with parameters!! (it even implement operator overloading!)

    but The best I tried I couldn't implement paremeterized constructor and operator overloading for my own value struct.. :(

    Can someone show me how to declare / implement a simple parameterized constructor (and operator overloading for '+') on my simple class above please?!!!!!!

    Thursday, December 20, 2012 1:04 PM

Answers

  • Lloyd:

    You aren't missing anything. You can't do this in C++/CX. As the documentation states: "A value struct or value class is a Windows Runtime-compatible POD ("plain old data structure"). It has a fixed size and consists of fields only..."   platform.winmd provides a special implementation of the Windows::Foundation::Size struct to make it easier to use _from_ C++/CX, but that doesn't mean you can do the same thing _in_ C++/CX.


    Thursday, December 20, 2012 10:54 PM

All replies

  • Hi, you can't, a value struct contains only public data fields, the parameterized constructor is defined as inline function in vccorlib.h.
    Thursday, December 20, 2012 3:35 PM
  • Yes, I saw that.

    I tried to defined such inline function myself. But they were not accepted by the compiler!

    like so:

    public value struct float2
    {
    	float x,y;
    };
    inline float2::float2(float _x, float _y)
    {
    	x = _x;
    	y = _y;
    }
    

    What am I missing??
    It seems to work for Windows::Foundation::Size::Size(float w, float h)!!

    Thursday, December 20, 2012 3:57 PM
  • Lloyd:

    You aren't missing anything. You can't do this in C++/CX. As the documentation states: "A value struct or value class is a Windows Runtime-compatible POD ("plain old data structure"). It has a fixed size and consists of fields only..."   platform.winmd provides a special implementation of the Windows::Foundation::Size struct to make it easier to use _from_ C++/CX, but that doesn't mean you can do the same thing _in_ C++/CX.


    Thursday, December 20, 2012 10:54 PM
  • Ho, they cheated again!!!
    Friday, December 21, 2012 9:08 AM
  • So does anyone know how developers can emulate the same behavior in our custom WinRT value struct types..?

    It clearly can't be defined in C++/CX, so then how can it be defined..? (Or is this magic truly only reserved for Microsoft?)

    (It certainly does make it easier to use such a struct with a custom constructor... : )

    Sunday, May 05, 2013 4:19 PM
  • About a year too late possibly for the response, but you may want to look at WRL as a possible means of implementing what you want. It's what C++/CX is built on.
    Friday, April 11, 2014 10:34 PM