2016-04-13 10 views
1

アンマネージドC++は別のオブジェクトに気軽に配置できるため、 は、そのようなアレイアンマネージドC++とマネージC++の間のポインタの相違点

int pt[50]; 
int* pointer = pt; 

へのポインタとして我々は、直接配列内の要素の最初の値を取得する*ポインタを使用することができます。 したがって、2番目の要素を指すために*(pointer ++)を使用することもできます。

しかし、^(ポインタ+ 5)を直接使用して配列の6番目の要素を取得できる場合は、 この例は次のとおりです。

array<int>^ pt = gcnew array<int>(50); 
int^ pointer = pt; 

どのようにポインタを媒体として使用して、配列内の別の要素にアクセスできますか?ここで

答えて

0

は、(アレイを横断する)内部ポインタ演算を使用しています...

使用であってもよく、わずかに異なるアプローチです。

これが役に立ちます。

using namespace System; 

ref class Buf 
{ 
    // ... 
}; 

int main() 
{ 
    array<Buf^>^ array_of_buf = gcnew array<Buf^>(10); 

    // Create a Buf object for each array position 
    for each (Buf^ bref in array_of_buf) 
    { 
     bref = gcnew Buf(); 
    } 

    // create an interior pointer to elements of the array 
    interior_ptr<Buf^> ptr_buf; 

    // loop over the array with the interior pointer 
    // using pointer arithmetic on the interior pointer 
    for (ptr_buf = &array_of_buf[0]; ptr_buf <= &array_of_buf[9]; ptr_buf++) 
    { 
     // dereference the interior pointer with * 
     Buf^ buf = *ptr_buf; 
     // use the Buf class 
    } 
} 

参照:https://msdn.microsoft.com/en-us/library/y0fh545k.aspx

// interior_ptr.cpp 
// compile with: /clr 
using namespace System; 

ref class MyClass { 
public: 
    int data; 
}; 

int main() { 
    MyClass^h_MyClass = gcnew MyClass; 
    h_MyClass->data = 1; 
    Console::WriteLine(h_MyClass->data); 

    interior_ptr<int> p = &(h_MyClass->data); 
    *p = 2; 
    Console::WriteLine(h_MyClass->data); 

    // alternatively 
    interior_ptr<MyClass ^> p2 = &h_MyClass; 
    (*p2)->data = 3; 
    Console::WriteLine((*p2)->data); 
} 
:ここで.NETのC++/CLIのVisual C++言語(PG 99)

から別の例であります

関連する問題