Sunday, July 26, 2009

How do I resize an array passed as an argument in C++/CLR?

In .net 2 C++ I want to pass an array to a function and have the size of the array set by that function.





however the following does not work.





bool functionF(array%26lt;System::String^%26gt;^ a)


{


a=gcnew array%26lt;System::String^%26gt;(100);


for(int i=0; i%26lt;100; i++)


{


a[i]=i;


}


return true;


}





int main()


{


array%26lt;System::String^%26gt;^ a=gcnew array%26lt;System::String^%26gt;(1);


bool ok=functionF( a);


Console::Writeline(a-%26gt;Length);


}





Nor does





bool functionF(array%26lt;System::String^%26gt;^ a)


{


array::resize(a,100);


for(int i=0; i%26lt;100; i++)


{


a[i]=i;


}


return true;


}





int main()


{


array%26lt;System::String^%26gt;^ a=gcnew array%26lt;System::String^%26gt;(1);


bool ok=functionF( a);


Console::Writeline(a-%26gt;Length);


}

How do I resize an array passed as an argument in C++/CLR?
I think you should be passing by reference not by value. When you return the unction operation goes out of scope so you lose the value


No comments:

Post a Comment