托管 C++ 中的全局或指针用于定制课程
我的项目既管理又管理非托管代码。我在托管代码中编写了一个 cIVR 类,同时在非托管 cpp 文件中定义了它。该项目已经运行良好一段时间了。 现在我需要从同一文件中的另一个函数调用数组“arrChannels”中对象之一的成员函数。
在数组定义期间,由于托管代码,它不允许我声明为静态,因此我想在全局指针中传递对数组的引用,以便我可以访问整个文件中的成员函数。
我正在使用 .Net 2008。
main() {
array<cIVR^>^ arrChannels = gcnew array<cIVR^>(num_devices); //MAXCHAN
for(int i=0; i< num_devices; i++) { //MAXCHAN
arrChannels[i] = gcnew cIVR();
}
我想要类似 -> 的东西
cIVR *ch; //全球的 ch = arrChannels; //上面代码中的最后一个
func2(int index) {
ch[index]->setNumber("123");
}
My project has both managed & unmanaged code. I wrote a class cIVR in managed code, while defining it in an unmanaged cpp file. The project has been working fine for some time.
Now I need to call a member function of one of objects in array 'arrChannels' from another function in the same file.
During array definition it's not allowing me to declare as static due to managed code, so I want to pass a reference to my array in a global pointer so I can access member function throughout the file.
I am using .Net 2008.
main() {
array<cIVR^>^ arrChannels = gcnew array<cIVR^>(num_devices); //MAXCHAN
for(int i=0; i< num_devices; i++) { //MAXCHAN
arrChannels[i] = gcnew cIVR();
}
I want some thing like ->
cIVR *ch; //global
ch = arrChannels; //last in above code
func2(int index) {
ch[index]->setNumber("123");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了“拥有全局通常不是一个好主意问题”之外,正确初始化
ch
的位置将在创建数组后的main()
内部。我还强烈建议您为您正在使用的函数使用正确的原型,例如:
和
Aside from the 'having a global is generally not a good idea issue', the place to correctly initialise
ch
would be insidemain()
after you created the array.I'd also strongly recommend using the correct prototypes for the functions that you're using, for example:
and