4

私は、C# で何をすべきかを知っているマネージ C++ KeyValuePair の質問をさらにもう 1 つ持っていますが、マネージ 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 のキーと値を設定する方法を教えてもらえますか?

4

2 に答える 2

3

これはそれを行う必要があります:

KeyValuePair< String ^, String ^> k(gcnew String("Foo"), gcnew String("Bar"));

KeyValuePair は不変型であるため、すべてをコンストラクターに渡す必要があります。これは、オブジェクトがスタック上にある場合にこのように記述することを除いて、C# と同じように見えます。

于 2008-12-05T16:07:20.237 に答える
1

試す

System::Collections::Generic::KeyValuePair< System::String^, System::String^>^ k = gcnew System::Collections::Generic::KeyValuePair< System::String^, System::String^>(gcnew System::String("foo") ,gcnew System::String("bar"))   ;
于 2008-12-05T16:19:21.413 に答える