gpt4 book ai didi

c++ - 访问命名索引属性

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:45 25 4
gpt4 key购买 nike

我正在使用一个 DLL,它提供一个类,该类具有名为 Tag 的命名索引属性

以下 C# 代码运行良好

// create an instance of the class 'ticks'
...

// set tag value to 46 for contract
ticks.set_Tag("contract",46 );

// get tag value for contract
int idx = (int)ticks.get_Tag("contract");

我想在 C++/CLI 中使用它

set_Tag 和 get_Tag 方法不可见

这段代码可以正常工作(或者至少可以编译)来设置值

ticks->Tag["contract"] = 46;

但是访问值编译失败

int idx = ticks->Tag["contract"];

error C2440: 'initializing' : cannot convert from 'System::Object ^' to 'int'

如果我强制(强制转换)它为一个 int,它包含垃圾

最佳答案

这可能是因为 idx 是一个装箱的 Int32,而不是 int。你可以使用

int idx = safe_cast<int>(ticks->Tag["contract"]);

unbox数。

关于c++ - 访问命名索引属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18381819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com