HOW TO:多載具有內部指標與原生指標的函式 (C++/CLI)
可多載函式,根據參數型別是內部指標或原生指標。
重要
此功能的語言支援的/clr編譯器選項,但非/ZW編譯器選項。
範例
程式碼
// interior_ptr_overload.cpp
// compile with: /clr
using namespace System;
// C++ class
struct S {
int i;
};
// managed class
ref struct G {
int i;
};
// can update unmanaged storage
void f( int* pi ) {
*pi = 10;
Console::WriteLine("in f( int* pi )");
}
// can update managed storage
void f( interior_ptr<int> pi ) {
*pi = 10;
Console::WriteLine("in f( interior_ptr<int> pi )");
}
int main() {
S *pS = new S; // C++ heap
G ^pG = gcnew G; // common language runtime heap
f( &pS->i );
f( &pG->i );
};
Output
in f( int* pi )
in f( interior_ptr<int> pi )