gpt4 book ai didi

c++ - 将字符串从托管列表框拉到 char* 类型的类变量中

转载 作者:行者123 更新时间:2023-11-28 07:27:32 24 4
gpt4 key购买 nike

这令人困惑。我理解指针,但跟踪引用是什么?
即使在 wiki 上,关于该做什么的说明也不是很清楚,更不用说我从来没有在 adv C++ 中教过这个。那么,如何将列表框中的项目放入我的一个类中呢?
另外,我能否得到一个简短的跟踪引用摘要以供将来引用?

我的表单有一个名为 listbox2 的列表框,其中包含一些数据。

我的名为“ManifistOBJ”的类有一个名为“setFilename(char*)”的方法

现在在其他程序中,我可以轻松地将对象添加到“AddFilename”方法,但我该如何做才能获得跟踪引用?

Sofar iv 尝试过:

DMManifest newmanifest = DMManifest();
for(int i =1;i< listBox2->Items->Count;i++)
{
ManifistOBJ newobj = ManifistOBJ();
System::String^ temp = listBox2->Items[i]->ToString();
String temp1 = temp;//?
char* temp2 = temp1.c_str();
newobj.setFilename(temp2);
newmanifest.push_back(newobj);
}

有了字符串旁边的 ^,我无法取消引用它。我不知道该怎么做。我可以让该方法接受一个字符串^,但这会弄乱我使用该库的其他程序。

最佳答案

#include <msclr/marshal_cppstd.h>

System::String^ temp = listBox2->Items[i]->ToString();

std::string temp1 = msclr::interop::marshal_as< std::string >( temp );

C++/CLI Converting from System::String^ to std::string

关于c++ - 将字符串从托管列表框拉到 char* 类型的类变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18453907/

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