gcroot 是什么意思?我在我正在阅读的代码中找到了它。
gcroot 是一个 C++/cli 模板类,可以轻松地在 C++/cli 类中保存托管类型。
例如,您可以拥有以下内容:
#include <msclr/gcroot.h>
using namespace msclr;
class Native {
public:
Native(Object ^obj) :
netstring(obj->ToString()) { // Initializing the gcroot<String ^>
}
~Native() {
}
void Print() {
array<Char> ^chars = netstring->GetChars(); // Dereferencing the gcroot<String ^>
_wprintf("netstring is:");
if (chars->Length > 0) {
pin_ptr<Char> charptr = &(chars[0]);
_wprintf("%s", (wchar_t const *)charptr);
}
}
private:
gcroot<String ^> netstring;
};
gcroot 充当托管对象或值类型实例的引用,并在复制对象或值类型实例时完成所有工作。
通常您需要使用 GCHandle 和 .NET 框架的一些 C 函数。这一切都封装在 gcroot 中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)