Platform :: String ^是否直接绑定到const char []?

时间:2015-04-21 09:30:27

标签: win-universal-app c++-cx

Platform::String^字符串中创建const char[]时;即

auto platformString = ref new Platform::String("MyString");

它是直接绑定到编译时const char[N]字符串还是分配一个副本来引用?

1 个答案:

答案 0 :(得分:1)

它需要一个副本 - 它必须,因为Platform::String^的生命周期由ref-count决定,而不是由普通的C ++生存期规则决定。

auto greeting = std::make_unique<wchar_t[]>(20);
wcscpy_s(greeting.get(), 20, L"Hello, World");
auto s = ref new Platform::String(greeting.get());
greeting.reset();

// Still works, even though the original string is gone
OutputDebugString(s->Data());

如果查看VC include目录中的文件 vccorlib.h (例如 C:\ Program Files(x86)\ Microsoft Visual Studio 11.0 \ VC \ include )然后你会看到带const ::default::char16* __strArg的构造函数最终调用WindowsCreateString来获取副本。