如何:用内部指针和本机指针重载函数 (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 )

请参见

参考

interior_ptr (C++/CLI)