Thread: Returning an Array of Pointers to Objects

  1. #1
    Registered User
    Join Date
    Apr 2006

    Returning an Array of Pointers to Objects

    Haven't seen very many clear examples around but I was wondering what would be the correct syntax to return and catch an array of pointers to objects?

    For this particular program I am parsing a string into an array of object tokens, and using that array in another class. Stabbing in the dark gets me a compil error, so I was wondering if anyone has any help on the matter?

    My current function prototype for returning the array of pointers is:
    ObjectName* [] function( string something);
    My return line for the function is:
    return ( array);
    Where array is an array of pointers to objects

    And the function prototype for the function which uses that array:
    double function( Object* array[])
    The gist of the compile error is I either have an extra bracket/s or are missing some, somewhere Any help would be much appreciated.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    You can't return an array.
    double function( Object* array[])
    When you pass an array as an arguement, it's a reference. Meaning anything you do to it will affect the array you passed to the function.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2006
    Hehe, I really do need sleep Thanks for the heads up!

  4. #4
    Registered User
    Join Date
    Mar 2005
    There a couple of ways. One is with a pointer to a pointer to objects and use it as an array.

    PersonInfo** RetrieveArrayOfObjectsViaPtrToPtr(void);
    int main(int argc, char **argv)
    	PersonInfo** x = RetrieveArrayOfObjectsViaPtrToPtr();
    	delete x[0];
    	delete x[1];
    	return 0;
    PersonInfo** RetrieveArrayOfObjectsViaPtrToPtr(void)
    	PersonInfo** arr = new PersonInfo*[2];
    	arr[0] = new PersonInfo("one");
    	arr[1] = new PersonInfo("two");
    	return arr;
    class PersonInfo has a Draw member method.
    Don't forget to delete the pointer to pointers!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Another option is to return a vector, which may be better because it handles memory management on its own and therefore won't have a memory leak like xeddiex's code has.

    Passing the vector by reference is more common than returning one from a function, but either way will work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Of Pointers With Objects
    By TheTaoOfBill in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2007, 09:37 PM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Replies: 2
    Last Post: 01-29-2003, 03:32 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM