Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Returning array references

Returning array references

Scheduled Pinned Locked Moved Managed C++/CLI
data-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bisquic
    wrote on last edited by
    #1

    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() );

    B 1 Reply Last reply
    0
    • B bisquic

      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() );

      B Offline
      B Offline
      bisquic
      wrote on last edited by
      #2

      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?

      A 1 Reply Last reply
      0
      • B bisquic

        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?

        A Offline
        A Offline
        Andy Wieberneit
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups