Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String^ declaration in C++

As far as I am aware C++ string declaration follows the form:

std::string param;

I was walking through a code and realised the declaration of string is done this way:

System::String^ param;

Can anyone share light on this declaration?! Is this Microsoft Visual C++ string declaration or a special library which provides another alternative to using C++ string.

like image 298
Bitmap Avatar asked Jul 31 '11 16:07

Bitmap


People also ask

How do you declare string in C?

Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

How string is declared and initialized in C?

How to Initialize a String in C? In string3, the NULL character must be added explicitly, and the characters are enclosed in single quotation marks. 'C' also allows us to initialize a string variable without defining the size of the character array.

What is string definition in C?

In C programming, a string is a sequence of characters terminated with a null character \0 .

What is the syntax of string?

The read syntax for strings is an arbitrarily long sequence of characters enclosed in double quotes ( " ). Backslash is an escape character and can be used to insert the following special characters.


2 Answers

It's Microsoft-specific, and is part of a language they call C++/CLI. This syntax declares a Common Language Runtime (CLR) String variable (the same kind you get when you declare a string in C#). These are not directly interchangeable with the several C/C++ string types, but Microsoft provides marshalling facilities to convert CLR String objects to unmanaged strings and vice versa.

C++/CLI enables developers to create programs that bridge regular C++ classes/functions (otherwise called "unmanaged code") with CLR classes/functions (otherwise called "managed code"). Microsoft also exposes lower-level features of the CLR to C++/CLI, some that are exposed to C# too (like pointers), and some that aren't (like finer granularity over member access levels).

It's useful if you want to use an existing C or C++ library in a language like C# (by making the bindings in C++/CLI then exposing them to the CLR without going through P/Invokes), or if you want to port an existing unmanaged C/C++ library or application to a managed environment.

like image 179
zneak Avatar answered Oct 10 '22 12:10

zneak


This is C++/CLI syntax for a handle to an object on the managed heap.

like image 9
Stu Mackellar Avatar answered Oct 10 '22 12:10

Stu Mackellar