【发布时间】:2023-03-30 18:58:01
【问题描述】:
我需要一个将 std::vector 转换为 CLI 列表的函数
generic<typename T> where T:CliCommonObjectBase
List<T>^ Converter::ConvertDataBaseListToList(DBList<TMObject> list)
{
List<T>^ returnList = gcnew List<T>();
for (DBIterator<TMObject> iter = list.first(); !iter.done(); iter.next())
{
DBRef<TMObject> tempObject = *iter;
returnList->Add(gcnew T("BlaBla"));
}
return returnList;
}
来自 CliCommonObjectBase 的构造函数
CliCommonObjectBase(String^ objectRefString);
电话
ConvertDataBaseListToList<CliMeeting^>(getReadBase()->getTermine());
CliMeeting 继承 CliCommonObjectBase
我的问题是 gcnew T("BlaBla") 给出错误
【问题讨论】:
-
您需要为此使用自动
t = Activator::CreateInstance(typeof(T), "BlaBla");。我不是 100% 确定这些论点。 -
你必须在这里检查运行时异常
-
没有运行时异常
-
你确定?因为对我来说它不起作用:D
-
约束不够好。编译器只知道基类有一个接受字符串的构造函数。不能保证派生类也有一个。你需要一个工厂函数,example。