windows runtime - How to initialize c++/cx class with aggregate initialization? -
i have ref class:
namespace n { public ref class s sealed { public: property platform::string^ x; }; }
how initialize in place aggregate initializer? have tried:
n::s s1 = { %platform::string(l"text") };
but compiler says
error c2440: 'initializing': cannot convert 'initializer list' 'n::s'
also:
n::s s1 { %platform::string(l"text") };
and error is:
error c2664: 'n::s::s(const n::s %)': cannot convert argument 1 'platform::string ^' 'const n::s %'
this works standard c++ this:
struct t { wstring x; }; t x { l"test" };
i not want use constructor here.
i assume mean don't want public
constructor on projected winrt type -- no problem, can use internal
keyword mean "public inside c++ not exposed through interop". means can use native c++ types parameters if like:
namespace testing { public ref class mytest sealed { public: property string^ foo { string^ get() { return m_foo; } void set(string^ value) { m_foo = value; } } internal: // not compile if public, since wchar_t* isn't valid mytest(const wchar_t* value) { m_foo = ref new string(value); } private: string^ m_foo; }; } mainpage::mainpage() { // projected type not have constructor testing::mytest t{ l"hello" }; outputdebugstring(t.foo->data()); t.foo = "\nchanged"; outputdebugstring(t.foo->data()); }
also don't need have private
variable hold string -- use auto-property in original code -- prefer explicit. means if needed access string lot within c++ code provide internal
accessor function , not have go through vtable call @ it.
Comments
Post a Comment