C++/CLI剪輯

來源:互聯網
上載者:User

標籤:system   運行時   運行   open   margin   對象   ide   integer   回收   

1.本地類中包含託管類成員變數的情況

 1 #include<vcclr.h>     // 必須包含vcclr.h標頭檔 2  3 //傳入 4  5 A^ a = gcnew A(); 6  7 gcroot<A^> *pA = new gcroot<A^>(); 8  9 *pA = a;10 11 void *ptr = pA;12 13 B *b = new B(pA);  //c++類14 15 //還原16 17 gcroot<A^> *  m_pA = (gcroot<A^> *)pA;18 19 (*m_pA)->FuncA(); //可調用A類介面;

 2.pin_ptr是防止您的對象移動將在記憶體回收堆的內部指標. 也就是說釘住指標的值不是由通用語言執行平台更改. 當向非託管函數傳遞託管類的地址時,這很有用,因為在解析非託管函數調用的過程中,該地址不會意外更改.

pin_ptr無法使用方式(在pin_ptr的生命週期內有效):

    • 函數參數

    • 作為函數的傳回型別。

    • 類成員的聲明

    • 目標的類型約束。

pin_ptr 數組的第一個元素的位置
 1 // pin_ptr_1.cpp   2 // compile with: /clr    3 using namespace System;   4 #define SIZE 10   5    6 #pragma unmanaged   7 // native function that initializes an array   8 void native_function(int* p) {   9    for(int i = 0 ; i < 10 ; i++)  10     p[i] = i;  11 }  12 #pragma managed  13   14 public ref class A {  15 private:  16    array<int>^ arr;   // CLR integer array  17   18 public:  19    A() {  20       arr = gcnew array<int>(SIZE);  21    }  22   23    void load() {  24    pin_ptr<int> p = &arr[0];   // pin pointer to first element in arr  25    int* np = p;   // pointer to the first element in arr  26    native_function(np);   // pass pointer to native function  27    }  28   29    int sum() {  30       int total = 0;  31       for (int i = 0 ; i < SIZE ; i++)  32          total += arr[i];  33       return total;  34    }  35 };  36   37 int main() {  38    A^ a = gcnew A;  39    a->load();   // initialize managed array using the native function  40    Console::WriteLine(a->sum());  41 }  
View Code
內部指標轉換為釘住的指標,並且,類型為返回 address-of 運算子 (&) 是內部指標,當運算元在託管堆時
 1 // pin_ptr_2.cpp   2 // compile with: /clr   3 using namespace System;   4    5 ref struct G {   6    G() : i(1) {}   7    int i;   8 };   9   10 ref struct H {  11    H() : j(2) {}  12    int j;  13 };  14   15 int main() {  16    G ^ g = gcnew G;   // g is a whole reference object pointer  17    H ^ h = gcnew H;  18   19    interior_ptr<int> l = &(g->i);   // l is interior pointer  20   21    pin_ptr<int> k = &(h->j);   // k is a pinning interior pointer  22   23    k = l;   // ok  24    Console::WriteLine(*k);  25 };  
View Code
釘住的指標轉換為另一種類型
 1 // pin_ptr_3.cpp   2 // compile with: /clr   3 using namespace System;   4    5 ref class ManagedType {   6 public:   7    int i;   8 };   9   10 int main() {  11    ManagedType ^mt = gcnew ManagedType;  12    pin_ptr< int > pt = &mt->i;  13    *pt = 8;  14    Console::WriteLine(mt->i);  15   16    char *pc = ( char* ) pt;  17    *pc = 255;  18    Console::WriteLine(mt->i);  19 }  
View Code

 

C++/CLI剪輯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.