Returning array references
-
I would like to have a function return a reference to a managed array. I would also like to be able to supply it as a parameter to another function. Is this possible? Something like this:
int anArray __gc[]; int getArray () __gc[]; int getArray () __gc[] { return anArray; } void processArray( int arrayRef __gc[] ) { // do something to anArray through arrayRef } ... processArray( getArray() );
-
I would like to have a function return a reference to a managed array. I would also like to be able to supply it as a parameter to another function. Is this possible? Something like this:
int anArray __gc[]; int getArray () __gc[]; int getArray () __gc[] { return anArray; } void processArray( int arrayRef __gc[] ) { // do something to anArray through arrayRef } ... processArray( getArray() );
Ok, here is something more specific... I have a class, class1, that contains 2 arrays. Let's call them array1 and array2. I am instantiating 3 objects of another class, class2, from class1. To the first two, I am passing array1 to the constructor, and to the third, I am passing array2. I would like to have these three new objects hold a reference to the arrays in the main class instead of holding their own copy. So in the constructor I'd like to do something like: Class2( int arrayRef __gc[] ) { this->arrayRef= &arrayRef; } or maybe: Class2( int (*arrayRef) __gc[] ) { this->arrayRef= arrayRef; } Either way, when I try to declare an array pointer in the header file of class2 like: int (*arrayRef) __gc[]; I get the following compile error: "cannot declare interior __gc pointer or reference as a member of class2" So, I'm thinking it is not even possible to have a class hold a private reference to an array of another class. Is there some way to do this?
-
Ok, here is something more specific... I have a class, class1, that contains 2 arrays. Let's call them array1 and array2. I am instantiating 3 objects of another class, class2, from class1. To the first two, I am passing array1 to the constructor, and to the third, I am passing array2. I would like to have these three new objects hold a reference to the arrays in the main class instead of holding their own copy. So in the constructor I'd like to do something like: Class2( int arrayRef __gc[] ) { this->arrayRef= &arrayRef; } or maybe: Class2( int (*arrayRef) __gc[] ) { this->arrayRef= arrayRef; } Either way, when I try to declare an array pointer in the header file of class2 like: int (*arrayRef) __gc[]; I get the following compile error: "cannot declare interior __gc pointer or reference as a member of class2" So, I'm thinking it is not even possible to have a class hold a private reference to an array of another class. Is there some way to do this?
If you want to have an array of integers in a managed class, you would normally do something like this:
__gc class MyClass
{
private:
int myArray[];
public:
int* getArray()
{
return myArray;
}
};You don't need to specify everything as __gc. If your class is already managed, its members are managed automatically.