亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++超集C++/CLI模塊的基本類型

 更新時間:2022年07月04日 11:12:36   作者:天方  
這篇文章介紹了C++超集C++/CLI模塊的基本類型,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

數(shù)值類型

對于基本的數(shù)值類型,在C++/CLI中是可以直接映射為托管類型的數(shù)值的,可以同時應(yīng)用于托管類型和非托管類型,編譯器會將其自動轉(zhuǎn)換。

基本類型

System命名空間中對應(yīng)的類

注釋/用法

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 = '\0';

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

long lval = ival;

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

System::Double

double d = 3.14159;

long double

System::Double

long double d = 3.14159L;

字符串

字符串CLI已經(jīng)內(nèi)置了:System::String,但C++的常用字符串有char*、wchar_t*、std::string等好多種,編譯器提供了char*、wchar_t*到System::String的自動轉(zhuǎn)換:

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

另外,也可以使用gcnew創(chuàng)建托管字符串:

System::String^ s = gcnew String("hello worold");

但是,對于System::String轉(zhuǎn)char*,系統(tǒng)沒有直接的語法支持。方法有很多種,我通常使用如下方式來轉(zhuǎn)換:

IntPtr ip = Marshal::StringToHGlobalAnsi(str);
const char* ch = static_cast<const char*>(ip.ToPointer());
//do something with ch
Marshal::FreeHGlobal(ip);

這里有個需要注意的地方是在使用完轉(zhuǎn)換出來的const char*后需要釋放掉轉(zhuǎn)換過程中的Intptr,如果沒有太多需要考慮性能的地方,大可以使用一個std::string將其拷貝走,寫成如下函數(shù)形式:  

    #include <string>

    using namespace std;
    using namespace System;
    using namespace System::Runtime::InteropServices;

    string cast_to_string(String^ str)
    {
        IntPtr ip = Marshal::StringToHGlobalAnsi(str);
        const char* ch = static_cast<const char*>(ip.ToPointer());
        string stdStr = ch;
        Marshal::FreeHGlobal(ip);

        return stdStr;
    }

 參考文章:如何:使用 C++ 互操作封送 ANSI 字符串

結(jié)構(gòu)體

除了基本類型外,有時我們也需要對結(jié)構(gòu)體進行映射,MS也提供了相應(yīng)的映射函數(shù),非常方便。具體可參考MSDN文章擴擴展封送處理庫,這里就不多介紹了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論