none
C++/CX question about strings RRS feed

  • Question

  • Hello I am a long time C# programmer in the industry and I am trying to transition into C++/CX as I have been writting Windows Phone 7/7.5 apps since day 1 of them being available. I always wanted to dive into c++ but being as I write c#/asp/asp.net/mvc/winform code for a living I never had the need to do so until now. Basically I am trying to figure out how to expose a "public" string for say the name of an object. code below ..

    #include "pch.h"

    #include <string>

    ref class TestClass sealed
    {
    public:
      property Platform::String^ ObjectName;
    };

    Now .. I seen during //BUILD/ they said not to really use Platform::String as it wont port to other platforms that use say std::wstring .. What should I be using as I cannot use std::wstring or any other standard string types as propertys in c++/cx or as public access that I can see. Also what would you use for a PUBLIC string type if I try to use the following I get a "a non-value type cannot have any public data members 'Name'"

    #include "pch.h"

    #include <string>

    ref class TestClass sealed
    {
    public:
      std::wstring Name;
    };

    I am just trying to wrap my head around what we take for granted in c# which is auto get/set's hah. I am sure this is a simple thing to do but would appreciate any help with this.

    Friday, November 9, 2012 4:18 PM

Answers

  • "Also do I have to do any dispose or delete on these RT objects to make sure they go out of scope when the class is released from GC cuz I think they are managed still right? or what."

    They're not "managed" as C++/CX produces native code and doesn't use .NET. But WinRT objects are referenced counted and C++/CX implements automatic reference counting so in general you don't have to do anything to release objects. Occasionally you may need to set a reference to nullptr but this isn't common.

    You may want to read this page: http://msdn.microsoft.com/en-us/library/windows/apps/hh699870.aspx, specifically the sections "Memory Management" and "Destructors".

    You may also want to read a bit about smart pointers (http://en.wikipedia.org/wiki/Smart_pointer) because that's what C++/CX uses for reference counting under the covers.

    Saturday, November 10, 2012 7:05 AM
    Moderator
  • The advice about using something std::wstring applies to generic purpose code that can/needs to be portable.

    When you create a C++/CX class like TestClass (it's "ref class" instead of just "class") then you need to use Platform::String in public properties because that's the only thing WinRT understands.

    Friday, November 9, 2012 6:50 PM
    Moderator

All replies

  • Oh and  property Platform::String^ ObjectName; does work just fine but just want to make sure I should be using that or if I can use a native c++ type instead for public strings.

    Friday, November 9, 2012 4:20 PM
  • The advice about using something std::wstring applies to generic purpose code that can/needs to be portable.

    When you create a C++/CX class like TestClass (it's "ref class" instead of just "class") then you need to use Platform::String in public properties because that's the only thing WinRT understands.

    Friday, November 9, 2012 6:50 PM
    Moderator
  • Ya from what I can tell you are 100% correct on this .. Ok not a big deal just making sure I am not going off in the wrong direction from the get go. Also do I have to do any dispose or delete on these RT objects to make sure they go out of scope when the class is released from GC cuz I think they are managed still right? or what.
    Friday, November 9, 2012 7:17 PM
  • "Also do I have to do any dispose or delete on these RT objects to make sure they go out of scope when the class is released from GC cuz I think they are managed still right? or what."

    They're not "managed" as C++/CX produces native code and doesn't use .NET. But WinRT objects are referenced counted and C++/CX implements automatic reference counting so in general you don't have to do anything to release objects. Occasionally you may need to set a reference to nullptr but this isn't common.

    You may want to read this page: http://msdn.microsoft.com/en-us/library/windows/apps/hh699870.aspx, specifically the sections "Memory Management" and "Destructors".

    You may also want to read a bit about smart pointers (http://en.wikipedia.org/wiki/Smart_pointer) because that's what C++/CX uses for reference counting under the covers.

    Saturday, November 10, 2012 7:05 AM
    Moderator