Build a bridge between C + + and C + + with/CLI (iii)--Basic type

Source: Internet
Author: User

Numeric type

For basic numeric types, a numeric value that can be mapped directly to a managed type in C + +/CLI can be applied to both managed and unmanaged types, and the compiler automatically transforms it.

basic type

system Span style= "font-family: Microsoft Jacob Black;" > The corresponding class in the namespace

note /

bool

system::boolean

bool dirty = false;

char

system::sbyte

char sp = ";

signed char

system::sbyte

signed char ch =-1;

unsigned char

system::byte

unsigned char ch = ' + ';

wchar_t

system::char

wchar_t wch = ch;

short

system::int16

short s = ch;

unsigned short

system::uint16

unsigned short s = 0xffff;

int

system::int32

int ival = s;

unsigned int

system::uint32

unsigned int ui = 0xFFFFFFFF;

long

system::int32

unsigned long

system::uint32

unsigned long ul = UI;

long long

system::int64

long long etime = UI;

unsigned long long

system::uint64

unsigned long long mtime = etime;

float

system::single

float f = 3.14f;

double

double d = 3.14159;

Long double

System::D ouble

Long double d = 3.14159L;

String

The

String CLI has been built in: system:: string , but commonly used strings for C + + are char*, wchar_t*, std:: There are many kinds of string, the compiler provides char*, wchar_t* to System:: Span style= "color: #2b91af;" >string automatic conversion for :

System::String^ s = "Hello worold";
System::String^ s2 = L "Hello worold";

Alternatively, you can use gcnew to create a managed string:

System::string^ s = gcnewstring("Hello worold");

However, for system ::String char*, there is no direct syntax support. There are many ways to do this, and I usually use the following method to convert:

intptr IP = marshal::stringtohglobalansi (     constchar* ch = static_cast<constchar*> (IP. ToPointer ());
    //do something with ch
marshal::freehglobal (IP);

There is a need to note that after the conversion of the const char* need to release the conversion process of IntPtr, if there is not much to consider the performance of the place, large can use a std::string copy it, written as follows function form:

#include <string>using namespacestd; using namespaceSystem; using namespacesystem::runtime::interopservices; stringCast_to_string (string^str) {INTPTR IP=Marshal::stringtohglobalansi (str); Const Char* ch = static_cast<Const Char*>(IP.        ToPointer ()); stringStdstr =ch;        Marshal::freehglobal (IP); returnStdstr; }

Reference article: how to: Marshal ANSI Strings using C + + Interop

Build a bridge between C + + and C + + (iii)--Basic type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.