views:

136

answers:

1

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.

Kindly help, I use several code from Google but have been unsuccessful. 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");  

}
A: 

Aside from the 'having a global is generally not a good idea issue', the place to correctly initialise ch would be inside main() after you created the array.

I'd also strongly recommend using the correct prototypes for the functions that you're using, for example:

int main()

and

void func2(int index)
Timo Geusch
but I am unable to use arrChannels from func2 ? its inaccessible due to scope
harisali
You're not supposed to use arrChannels from func2. You're supposed to _set_ ch to point to arrChannels in the constructor after you initialised the array there and access it via ch.
Timo Geusch
how i can do this ? plz guide. syntexi.e how I can use arrChannels via globle scope pointer through out in file ?
harisali
Actually you'll probably have to make arrChannels global as it's not a pointer per se but a garbage collected pointer. So, make arrChannels global, initialise it `main()` as you do now and then refer to it in func2 if you must do this via a global variable rather than factoring out this subset of code into its own object. IIRC you in order to access arrChannels via a plain C++ pointer you'd have to pin the array (which you can using `pin_ptr<>` but that would work against the garbage collection in .NET.
Timo Geusch