Beginner's String questions
In all the C++/CLI examples I've seen, strings seem to get allocated like this:-
Code:
String^ MyString = "Hello World";
To my (C++ mind) this feels more like char* than an actual String object. Doesn't C++/CLI have any actual string object type?
Also... if I have a simple int containing some value (lets say, the value 1234) what's the easiest way of allocating it to a String (such that the string contents would then become "1234") :confused:
Re: Beginner's String questions
This is the .net String class, boxed by ^ as it is a .net managed object. See https://docs.microsoft.com/en-us/dot...tframework-4.8
Re int. See https://docs.microsoft.com/en-us/dot...tframework-4.8
Note re the references. To see the examples using C++/cli, choose C++ from the top down language list top-right.
Re: Beginner's String questions
Quote:
Originally Posted by
2kaud
Thanks 2kaud - it looks like (in C++/CLI) the int type is actually an object type, rather than the simple type we know from regular C :thumb:
Re: Beginner's String questions
Quote:
Originally Posted by
John E
it looks like (in C++/CLI) the int type is actually an object type, rather than the simple type we know from regular C
That doesn't sound right to me because C++/CLI is an extension of standard C++ (just like C++ is an extension of C).
It means all fundamental types of C++ like int, float, char etcetera are available in C++/CLI as well.
Re: Beginner's String questions
Well here's what I needed to do to convert int to String^
Code:
int n = 56;
String^ s = n.ToString();
Re: Beginner's String questions
Quote:
Originally Posted by
John E
Well here's what I needed to do to convert
int to
String^
Code:
int n = 56;
String^ s = n.ToString();
I suppose this is an example of so called coersion polymorphism in the C++/CLI type system.
It would mean that n is an int in the C++ sense but it is implicitly cast to an equivalent C++/CLI managed type (probably System::Int32 or System::Int64) before the ToString() method is applied.
Re: Beginner's String questions
All types are derived from a base class in .net.
int and long for example are syntactic sugar for System.Int32 and System.Int64 classes.
In .Net string (or System.String) is a class.
When you make an assignment (C# syntax shown), the string object is created and the string value is assigned.
Code:
var myString = "Hello";
Btw, strings in .Net are immutable so the following creates a new string object that is assigned to myString:
Code:
myString += " World!";