locked
Can't use IVector<T> in my value struct?

    Question

  • I would like to have a vector of value structs in another value struct:

    public value struct HttpHeader
    {
       Platform::String^ mName;
       Platform::String^ mValue;
    
    };
    
    typedef Windows::Foundation::Collections::IVector<HttpHeader> IHttpHeaders;
    
    public value struct HttpRequestEvent
    {
       uint32 mHandle;
       IHttpHeaders^ mHeaders;
    
    };

    However I get this error on compilation:

    error C3992: 'mHeaders': signature of public member contains invalid type 'myNamespace::IHttpHeaders ^' 

    If this isn't possible, can someone recommend another way of implementing this?



    Thursday, August 02, 2012 9:13 PM

Answers

  • Hello,

    Value structs can only have public fields, only allow fundamental types and Strings as members. Parameters of type value struct can only be passed by value.

    Runtime classes/structs on the other hand have an internal ref-count, allow public/private/internal methods, properties & events as well as private/internal fields. Parameter of type runtime class get passed by reference.

    To understand more about runtime classes, start by reading this MSDN topic: http://msdn.microsoft.com/en-us/library/windows/apps/hh699870(v=vs.110).aspx

    Hope this helps,
    Marian Luparu
    Visual C++

    Thursday, August 02, 2012 11:42 PM

All replies

  • Hello,

    Value structs can only have public fields, only allow fundamental types and Strings as members. Parameters of type value struct can only be passed by value.

    Runtime classes/structs on the other hand have an internal ref-count, allow public/private/internal methods, properties & events as well as private/internal fields. Parameter of type runtime class get passed by reference.

    To understand more about runtime classes, start by reading this MSDN topic: http://msdn.microsoft.com/en-us/library/windows/apps/hh699870(v=vs.110).aspx

    Hope this helps,
    Marian Luparu
    Visual C++

    Thursday, August 02, 2012 11:42 PM
  • I would like to have a vector of value structs in another value struct:

    public value struct HttpHeader
    {
       Platform::String^ mName;
       Platform::String^ mValue;
    
    };
    
    typedef Windows::Foundation::Collections::IVector<HttpHeader> IHttpHeaders;
    
    public value struct HttpRequestEvent
    {
       uint32 mHandle;
       IHttpHeaders^ mHeaders;
    
    };

    However I get this error on compilation:

    error C3992: 'mHeaders': signature of public member contains invalid type 'myNamespace::IHttpHeaders ^' 

    If this isn't possible, can someone recommend another way of implementing this?




    请问这个问题有具体的代码可以参考下吗?
    Thursday, November 08, 2012 2:11 PM
  • A value struct can only have other value structs or intrinsic types as members. mHeaders in your example is not - it's a reference type. That's why you're getting the compile-time error.

    If you need to store a vector of interfaces, the underlying class should either be a standard C++ class or a ref class. It really depends on what you're planning to use this type for. If it's only for use internally in your code, make it a standard C++ class:

    struct HttpRequestEvent {  
        uint32 mHandle;  
        IHttpHeaders^ mHeaders;
    };

    If this needs to go over an WinRT boundary, make it a ref-class: 

    ref struct HttpRequestEvent {
        property uint32 Handle;
        property IHttpHeaders^ Headers;
    }; 

     

    Hope this helps,
    Marian Luparu
    Visual C++

    Thursday, November 08, 2012 6:46 PM