Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

edwinnie

returning an array of pointers???

This topic is 5528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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);


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Crystal* returnCrystals()
{
static Crystal crystals[length];
int item;

//do some kinda code

for (int i = 0; i < length; ++i)
crystals[ i ] = item;

return crystals;
}



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.
(adjust to reflect an array of pointers, later )

edit: my array made everything italic, lol

[edited by - Alpha_ProgDes on May 5, 2003 8:42:10 AM]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!!!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!