Returning an Array of Pointers to Objects

This is a discussion on Returning an Array of Pointers to Objects within the C++ Programming forums, part of the General Programming Boards category; Haven't seen very many clear examples around but I was wondering what would be the correct syntax to return and ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    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:
    Code:
    ObjectName* [] function( string something);
    My return line for the function is:
    Code:
    return ( array);
    Where array is an array of pointers to objects

    And the function prototype for the function which uses that array:
    Code:
    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
    Location
    Out of scope
    Posts
    4,069
    You can't return an array.
    Code:
    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
    Posts
    7
    Hehe, I really do need sleep Thanks for the heads up!

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

    Example:
    Code:
    PersonInfo** RetrieveArrayOfObjectsViaPtrToPtr(void);
    
    int main(int argc, char **argv)
    {
    	PersonInfo** x = RetrieveArrayOfObjectsViaPtrToPtr();
    
    	x[0]->Draw();
    	x[1]->Draw();
    
    	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
    Posts
    7,318
    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, 08: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, 02:32 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21