从Platform::String^
字符串中创建const char[]
时;即
auto platformString = ref new Platform::String("MyString");
它是直接绑定到编译时const char[N]
字符串还是分配一个副本来引用?
答案 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
来获取副本。