Reputation: 1960
I'm a complete noob in Visual C++ programming, so I don't know if this makes sense or not.
Well, I'm trying to change a label text to display in a form, so if I do this:
int value_a = 1;
int value_b = 2;
System::String^ j;
j = System::Convert::ToString(value_a) + ", " + System::Convert::ToString(value_b);
label1 -> Text = j;
It works perfectly, but when I tried doing this:
int value_a = 1;
int value_b = 2;
System::String^ j;
j = std::to_string(valor1) + ", " + std::to_string(valor2);
label1-> Text = j;
I get plenty of errors... What's the difference between using
string j;
or
System::String^ j;
and between the functions
System::Convert::ToString();
and
std::to_string();
????
Upvotes: 3
Views: 6078
Reputation: 7881
The source of your confusion is probably that you are using C++/CLI, so you have access to many different string types. C++/CLI was developed to give you access to both worlds:
System::String is the managed, .NET string type. This is the type of string that all .NET languages use (C#, VBA, ect). This is a class that will only be able to use with C++/CLI & .NET. Use this type if you are writing pure Windows code, or if want to write a .NET library. In these languages, the "to string" function is actually a member of all classes, and the cast is often implicit. You can use
j = valor1+","+valor2;
If you want to get explicit, look into the Int::ToString function:
System::String^ j= Int32(1).ToString();
std::string is the unmanaged, stl string. This is the standard C++ class, and is available to both C++ and C++/CLI. You will have to marshal this class if you want to use it with other .NET classes. Use this class if you are trying to stay completely in the unmanaged code.
If you are trying to learn C++, I'd suggest turning off C++/CLI for now, and sticking with the standard library string.
Upvotes: 0
Reputation: 63200
System::String^ j;
is a C++/CLI String which is Microsoft's implementation of C++ on top of the .NET framework, so you can communicate from it with .NET languages (C#, VB.NET) and still use C++. The ^
is a garbage collected pointer used by C++/CLI. This means that you do not have to care about cleaning up after yourself when having used gcnew
std::string
is the C++ Standard Library's version of a string. It's native C++ as some may call it.
In C++ each time you use new
with a pointer, yuck, you must not forget to call delete
.
System::Convert::ToString
is also C++/CLI from Microsoft.
std::to_string
would be the C++11 equivalent of that.
The implementations are quite different, so you cannot expect to get the same results with these different types.
Upvotes: 3