returning an array of pointers???

Started by
18 comments, last by edwinnie 20 years, 11 months ago
Hi, i need some help here in C/C++. Suppose if i want to return an array of pointers from a function, what should the prototype be? and the return type? eg: //----------- //in Player.h //----------- class Player { public: <####> GetCrystals(); //what should <####> be? private: Crystal* crystals[4]; } //------------- //in Player.cpp //------------- <####> Player::GetCrystals() //what should <####> be? { ... return crystals; } I would be grateful if anyone heelps. thx! Edwin
Advertisement
You have several choices:

One might be to let the caller pass a pointer to the array where you function stores the pointers:

void GetCrystals(Crystal *crystal_array);

Another is to simply return a pointer to your internal struct:

Crystal *GetCrystals(void);

Yet another is to use a vector (best, IMO):

void GetCrystals(std::vector<Crystal *> &crystal_vector);
std::vector<Crystal *> GetCrystals(void);

#### = Crystal**

[edited by - novum on May 5, 2003 8:16:34 AM]
for instance.

if you are returning an int from a function then the prototype would look like this:
int returnInteger (); 


so using that same train of thought, if you want to return an array of pointers to Crystal:
Crystal* returnCrystals (); 


hope that helps.

Beginner in Game Development?  Read here. And read here.

 

quote:Original post by Alpha_ProgDes
so using that same train of thought, if you want to return an array of pointers to Crystal:

Crystal* returnCrystals ();
Not necessarily wrong, but note that you are not returning any Crystal pointers, but only a pointer to an interal array not owned by the caller.

I don't really know if it is possible to return an array , ie. type[8] Function(void); but besides that your only choices are to return an object (std::vector, std::list, or your own kind).

In most cases I would argue using a STL vector, but that might not necessarily be the best solution in your case as I don't know your needs:
std::vector<Crystal *> GetCrystals(void){    std::vector<Crystal *> list;    for(int i = 0; i < sizeof(crystals); i++)        list.push_back(crystals[i]);    return list;} 



[edited by - CWizard on May 5, 2003 8:33:30 AM]
thx fer all the replies so fast!

looks like i missed out abit of info
suppose,


class Playarea
{
...
void Generate(<####> arrayptr_crystals)
{
}
}


so somewhere in the code, i would want to use the accessor function defined in Player class:

Generate( player->GetCrystals() );

//player is a ptr eg: Player* player;

the accessor function will return an array of pointers for usage in the Generate() function.

thx!

Edwin
 Crystal* returnCrystals() {    static Crystal crystals[length];    int item;    //do some kinda code    for (int i = 0; i < length; ++i)        crystals = item;<br><br>    return crystals;<br> }<br> </pre>  <br><br><br>if the array is static the function doesn't destroy the array as it's still in memory. i remember making a similar mistake in my C class. just in that class i didn't make the array static, it was just local. <br>(adjust to reflect an array of pointers, later <img src="smile.gif" width=15 height=15 align=middle>)  <br><br>edit: my array made everything italic, lol<br><br><SPAN CLASS=editedby>[edited by - Alpha_ProgDes &#111;n May 5, 2003 8:42:10 AM]</SPAN>    

Beginner in Game Development?  Read here. And read here.

 

Alpha_ProgDes, that is not a good solution. How will the caller know the number of elements in the array? You may know it now, but it is not good practice to rely on hardcoded numbers in that way. A better version of yours is:
std::vector<Crystal *> &Player::GetCrystals(void){    static std::vector<Crystal *> list;    list.clear();    for(int i = 0; i < sizeof(crystals); ++i)        list.push_back(crystals[i]);    return list;} 
Then you could keep the original crystal list in a vector as well, and simply return a reference to it.

Otherwise, you could work with these functions:

std::vector<Crystal *> Player::GetCrystals(void);
void Playarea::Generate(const std::vector<Crystal *> &crystal_list);

// and use them like this
play_area.Generate(player1.GetCrystals());

EDIT: minor HTML mistakes

[edited by - CWizard on May 5, 2003 8:58:17 AM]
i must say, i am not realli gd at STL yet,
so i kept getting errors like "vector is not a member of ''std''"?

hmm...i might have noticed:

void GetCrystals(std::vector &crystal_vector);
std::vector GetCrystals(void);

or

std::vector Player::GetCrystals(void);


hmm... return type is a std::vector? or std::vector?

and how come function arguments are different? one is void and the other is std::vector &crystal_vector?

heelps!!

Edwin
Wait a minute... Aren''t arrays global? IE, couldn''t you do something like this:

  int main{     Crystal* array[5];     GetCrystal();     return 0;     }void GetCrystal();     {     for(int i = 0; i < 5; ++i)         {         array[i] = &Something[i];         }      }  


Of course, I could be totally wrong.


Curse you windows and your poor explanation of my errors!!!
Curse you windows and your poor explanation of my errors!!!

This topic is closed to new replies.

Advertisement