std::string to System::String conversion
-
Hi, I have
System::String ^ St = gcnew System::String("Hey ho!");
//...
std::string str("standard");St = str; //This line won't work, but it's what I'd like to achieve
The only way I can make it work is by doing gcnew again
St = gcnew System::String(str.c_str());
but I'm guessing this is not the right way. All the examples I have found tell how to do this when creating a new instance ofSystem::String
but none how to do it with an existing one. -
Hi, I have
System::String ^ St = gcnew System::String("Hey ho!");
//...
std::string str("standard");St = str; //This line won't work, but it's what I'd like to achieve
The only way I can make it work is by doing gcnew again
St = gcnew System::String(str.c_str());
but I'm guessing this is not the right way. All the examples I have found tell how to do this when creating a new instance ofSystem::String
but none how to do it with an existing one. -
System::String()
has no constructor that accepts astd::string
Try this:St = str->c_str();
I must get a clever new signature for 2011.
-
Tried it, no good.
error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'const char *' to 'System::String ^'
-
Sorry, I guess there is no simple way to do this other than :
St = gcnew System::String(str.c_str());
I must get a clever new signature for 2011.
-
Fair enough. That's what I'd done, but... am I not re-allocating memory without freeing it first?? (I have to do it for a dozen of strings!)
piul wrote:
am I not re-allocating memory without freeing it first?
It depends on the actual structure of your code. I think a more pertinent question would be: why are you using both
std::string
andSystem::String
when you only really need one of them, probably the latter. For example code such as:std::string str("Some string");
System::String ^ st = gcnew(str.c_str());serves no real purpose, in that the
std::string
is totally redundant.I must get a clever new signature for 2011.
-
Hi, I have
System::String ^ St = gcnew System::String("Hey ho!");
//...
std::string str("standard");St = str; //This line won't work, but it's what I'd like to achieve
The only way I can make it work is by doing gcnew again
St = gcnew System::String(str.c_str());
but I'm guessing this is not the right way. All the examples I have found tell how to do this when creating a new instance ofSystem::String
but none how to do it with an existing one.