locked
How do a pass an array of ints to a public member of a ref class in C++/CX

    Question

  • Using public member in C++/CX ref classes so far seems to be a big pain. I cannot pass or return almost anything to those methods apparently to be callable from C#, VB etc, which I do not care. I do not intend to use C# or VB etc., neither I want to export library etc.

    I want to pass an array of integers to a public method of a ref class, something like this...

    ParamChanged(Platform::Collections::Vector<float> params);

    Now I get this error...

    Error 1 error C3986: 'ParamsChanged': signature of public member contains native type 'std::equal_to<_Ty>' c:\...\filtercontrolinterface.h

    I get this error for almost anything I try to pass, other than just plain "int"s or other ref class pointers. How do I possibly pass an array now? 

    I mean I can pass a "int *", but I do not like this solution.


    • Edited by pragos Monday, August 6, 2012 8:39 AM
    Monday, August 6, 2012 8:31 AM

Answers

  • Non C++/CX types cannot be passed across the ABI boundary. Therefore, a C++/CX class can only have ABI-safe public methods.

    internal is correct for C++ code that you don't want to be ABI-safe yet still visible to other classes in the namespace.

    Sorry, there isn't yet documentation that explains in detail the C++/CX access modifiers. Hopefully with the next document dump.

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 6:59 AM
    Monday, August 13, 2012 9:18 PM
    Moderator
  • C++/CX access modifiers are documented in these locations:

    Btw, if you're planning to only use the classes internally into your project, why not author standard C++ classes and use classic ways of passing data around without the WinRT constraints? Do you ever need to pass this data over to another WinRT component at any point?

    Thanks,
    Marian Luparu
    Visual C++

    Monday, August 13, 2012 10:49 PM
  • Thanks Steve and Marian for your responses.

    I am using ref classes basically for 2 reasons...

    1. I have a couple of UserControl classes which want to pass back and forth though my other classes. Since UserControl classes are ref classes, I cannot seem to use them in standard C++ classes, although other way round is possible.

    2. I like the reference counting feature in ref classes, compared to "new" "delete" in standard C++. So, I am trying to use ref classes consistently through out my project.

    The "internal" works for me.

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 6:59 AM
    Tuesday, August 14, 2012 3:11 AM

All replies

  • Hi,

    From the document http://msdn.microsoft.com/en-us/library/windows/apps/br212455.aspx, I think we should be using float32 or float64 instead of just float in C++/CX.

    -Sagar


    Monday, August 6, 2012 6:33 PM
    Moderator
  • void ParamChanged(Platform::Collections::Details::WFC::IVector<float>^ params);

    Tuesday, August 7, 2012 1:29 AM
    Moderator
  • I think I need to use the access modifier "internal:" instead of "public:" in my ref classes to use them like a standard C++ public member. Then I can pass anything I want and use these classes within my project.
    Tuesday, August 7, 2012 5:04 AM

  • Hello,

    As far as I know internal is meaning for Not published in metadata
    http://msdn.microsoft.com/en-us/library/windows/apps/hh755822(v=vs.110).aspx

    If you want to create a components, you should consider this.

    For more information, please check this document.
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh441569(v=vs.110).aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, August 7, 2012 7:52 AM
  • Thanks Jesse for responding.

    Actually for this project I do not want to publish my components, I just want to use the classes in the same project. As a Standard C++ programmer, I was expecting that "public" keyword will behave the same way as it does in Standard C++. But seems like I can use "internal" instead of "public" for this purpose. And use "public" only when I want to expose my component. However, I haven't been able to locate any documentation explaining the access modifiers in C++/CX in details.

    Tuesday, August 7, 2012 8:13 AM
  • Non C++/CX types cannot be passed across the ABI boundary. Therefore, a C++/CX class can only have ABI-safe public methods.

    internal is correct for C++ code that you don't want to be ABI-safe yet still visible to other classes in the namespace.

    Sorry, there isn't yet documentation that explains in detail the C++/CX access modifiers. Hopefully with the next document dump.

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 6:59 AM
    Monday, August 13, 2012 9:18 PM
    Moderator
  • C++/CX access modifiers are documented in these locations:

    Btw, if you're planning to only use the classes internally into your project, why not author standard C++ classes and use classic ways of passing data around without the WinRT constraints? Do you ever need to pass this data over to another WinRT component at any point?

    Thanks,
    Marian Luparu
    Visual C++

    Monday, August 13, 2012 10:49 PM
  • Thanks Steve and Marian for your responses.

    I am using ref classes basically for 2 reasons...

    1. I have a couple of UserControl classes which want to pass back and forth though my other classes. Since UserControl classes are ref classes, I cannot seem to use them in standard C++ classes, although other way round is possible.

    2. I like the reference counting feature in ref classes, compared to "new" "delete" in standard C++. So, I am trying to use ref classes consistently through out my project.

    The "internal" works for me.

    • Marked as answer by Jesse Jiang Friday, August 24, 2012 6:59 AM
    Tuesday, August 14, 2012 3:11 AM
  • Hi pragos,

    While that approach will work, it is not how we recommend using C++/CX. See my second reply on this thread "The goal of C++/CX...": http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/b9c8a6d0-d6e8-4300-ab04-ef42a332d92f

    To answer your specific questions:

    1. You should be able to have ^ members in standard C++ classes. The other way is equally possible. Please post back (or start another thread) with the issues you're seeing with ref class members inside a standard C++ class. The following code should work:

    ref class Foo {
    };
    
    struct StdClass {
    	Foo^ _f;
    };
    
    void method() {
    	Foo^ f1 = ref new Foo(10);
    	StdClass s;
    	s._f = f1;
    }

    2. std::shared_ptr and std::unique_ptr are the C++11 "replacements" for new/delete in standard C++, not WinRT refcounting. While as I said, it is possbile to use WinRT classes everywhere in your app, you will certainly pay for the additional overhead that WinRT adds when compared to std::shared_ptr usage.

    Hope this helps,
    Marian Luparu
    Visual C++

    Friday, August 24, 2012 5:42 PM