每當我嘗試直接分配一個字符串變量到另一個字符串變量時,我得到錯誤發現沒有合適的對話。
那麼有沒有辦法將String ^指針轉換爲非指針struct String?如何將String ^轉換爲C++ CLI中的字符串?
我想:
System::String a = System::String('X',256);
我不想:
System::String^ a = %System::String('X',256);
每當我嘗試直接分配一個字符串變量到另一個字符串變量時,我得到錯誤發現沒有合適的對話。
那麼有沒有辦法將String ^指針轉換爲非指針struct String?如何將String ^轉換爲C++ CLI中的字符串?
我想:
System::String a = System::String('X',256);
我不想:
System::String^ a = %System::String('X',256);
不,沒有,因爲漢斯在評論中指出,System::String
是不可改變的。你不能分配它。您只能將句柄與全新的System::String
對象相關聯。
BTW
System::String^ a = %System::String('X',256);
不正確,它應該是
System::String^ a = gcnew System::String('X',256);
感謝您讓我知道gcnew技巧 – 2013-03-10 07:33:01
使用System::String a('X', 256);
。
謝謝,但這不是轉換,它只是一個例子。如果我想將'System :: String a'分配給'System :: String b',該怎麼辦? – 2013-03-09 10:53:30
那你爲什麼不用'b = a'? – Oswald 2013-03-09 10:59:45
拋出錯誤'C3149:'System :: String':不能在沒有頂級'^'的情況下使用此類型 – 2013-03-09 11:01:32
系統::字符串是一個不變的引用類型。你不能改變這一點,你必須使用帽子。如果你不喜歡這個語法,那麼就不要使用C++/CLI,C#和VB.NET都不會在它們的語法中區分值和引用類型。 – 2013-03-09 13:18:06