在托管 C++ 中创建 KeyValuePair
我还有另一个托管 C++ KeyValuePair 问题,我知道在 C# 中该怎么做,但很难转换为托管 C++。 这是在 C# 中完成我想做的事情的代码:
KeyValuePair<String, String> KVP = new KeyValuePair<string, string>("this", "that");
我已将其反映到 MC++ 中并得到:
KeyValuePair<String __gc*, String __gc*> __gc* KVP = (S"this", S"that");
我正在将其翻译为:
KeyValuePair<String ^, String ^> KVP = (gcnew String("this"), gcnew String("that"));
我从我的 上一个问题 KeyValuePair 是一个值类型; 问题是它在C++中是值类型而在C#中是引用类型? 谁能告诉我如何从 C++ 设置 KeyValuePair 的键和值?
I have yet another managed C++ KeyValuePair question where I know what to do in C#, but am having a hard time translating to managed C++. Here is the code that does what I want to do in C#:
KeyValuePair<String, String> KVP = new KeyValuePair<string, string>("this", "that");
I've reflected it into MC++ and get this:
KeyValuePair<String __gc*, String __gc*> __gc* KVP = (S"this", S"that");
which I'm translating to:
KeyValuePair<String ^, String ^> KVP = (gcnew String("this"), gcnew String("that"));
I know from my previous question that KeyValuePair is a value type; is the problem that it's a value type in C++ and a reference type in C#? Can anyone tell me how to set the key and value of a KeyValuePair from C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以做到:
KeyValuePair< 字符串^,字符串^> k(gcnew String("Foo"), gcnew String("Bar"));
KeyValuePair 是不可变类型,因此您必须将所有内容传递给构造函数,这看起来与 C# 中相同,只不过您编写如果对象在堆栈上,就像这样。
This should do it:
KeyValuePair< String ^, String ^> k(gcnew String("Foo"), gcnew String("Bar"));
KeyValuePair is an immutable type, so you have to pass everything to the constructor, which looks the same as in C#, except you write it like this if the object is on the stack.
尝试
try