每当我尝试直接分配一个字符串变量到另一个字符串变量时,我得到错误发现没有合适的对话。
那么有没有办法将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