locked
About static member of ref class.

    Question

  • Hi,

    I write a WinRT component using C++/CX. I define a static member in a ref class in the component. VS prompts me: "IntelliSense: a member of a WinRT class cannot be of a non-WinRT class type". But the project can build and run normally. The method of the static member also can be called normally.

    I want to know that is there any limitation/tips to define a static member in a ref class. Thanks.

    demo code:

    namespace StaticMem
    {
        public ref class Class1 sealed
        {
        public:
            Class1();
    
        private:
            static CRITICAL_SECTION m_sLock; 
        };
    }


    • Edited by Lattimore Wednesday, October 23, 2013 7:48 AM
    Wednesday, October 23, 2013 7:45 AM

Answers

  • Hi Lattimore,

    Welcome back!

    Your code is correct. I test declare a static member on my VS and without a error prompt.

    A ref class can have standard C++ types, including const types, in any private, internal, or protected private members.

    You could refer this link:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh699870.aspx

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Lattimore Friday, October 25, 2013 11:52 AM
    Friday, October 25, 2013 7:01 AM
    Moderator

All replies

  • There's a solution proposed here:
    http://www.codeproject.com/Articles/262151/Visual-Cplusplus-and-WinRT-Metro-Some-fundamentals

    "...the reason you cannot have WinRT Objects with public functions that have native C++ arguments is that these objects would then not be consumable by non C++ code.  However, the (obvious?) solution is to make the constructor private and have the class that creates the Object declared as a 'friend' class (duh!).  Then all is well, the compiler is happy, and I am happy."


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, October 23, 2013 1:26 PM
    Moderator
  • Hi,

    I cannot find anything about static member of ref class in the article. I just want to know why the VS prompt me an error but the code can be built and run normally. And I want to know whether a ref class can have a private static member of native C++ type, not a reference or pointer. 

    Thursday, October 24, 2013 5:20 AM
  • Hi Lattimore,

    Welcome back!

    Your code is correct. I test declare a static member on my VS and without a error prompt.

    A ref class can have standard C++ types, including const types, in any private, internal, or protected private members.

    You could refer this link:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh699870.aspx

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Lattimore Friday, October 25, 2013 11:52 AM
    Friday, October 25, 2013 7:01 AM
    Moderator
  • Hi, 

    Thanks for confirming my question. I will ignore the prompt of my VS.

    Friday, October 25, 2013 11:52 AM