FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


C++ Return array from function





flatliner
I am just starting off in c++ and I have function which I want to return two pieces of information from. So I make an array in the function and return it, but you cannot do that in c++ right. So instead I use a pointer to refer to the array in memory and then increment it to get the values right. I have googled this topic and still cannot get it to work. Here is my function, well method, its inside a class.

Code:


int defencesiveAttack()
{
   
   int * pointer;
   int  info[1];
   pointer = &info[0];
   
   // working out how much user will hit
   srand((unsigned int)time(0));
   info[0] = (strenght+exp)*rand()%5+1;

   // working out the experience pionts for the hit
   info[1] = (info[0]/2)-exp;



   return pointer;
}


Code:


person player1;
playerAction = player1.defencesiveAttack()



I am abit lost after this bit. Could anyone help? thanks
AftershockVibe
Err... this post is going to be a little long but hopefully you'll learn a bit! Here goes...

Firstly, the reason your code won't compile is because you are returning a variable of type "int*" and it is declared to return a type int.

Your code should look like this:
Code:
int* defencesiveAttack()
{
   
   int * pointer;
   int  info[1];
   pointer = &info[0];
   
   // working out how much user will hit
   srand((unsigned int)time(0));
   info[0] = (strenght+exp)*rand()%5+1;

   // working out the experience pionts for the hit
   info[1] = (info[0]/2)-exp;

   return pointer;
}


Secondly, arrays are not real objects, which is why you can't return them. They're a hold-over from C. They have their place but really in this case you want to be returning a std::Vector or List.

Code:
std::Vector<int> defencesiveAttack()
{
   std::Vector<int> info = new std::Vector<int>();
   
   // working out how much user will hit
   srand((unsigned int)time(0));
   info.push_back((strenght+exp)*rand()%5+1);

   // working out the experience pionts for the hit
    info.push_back((info[0]/2)-exp);

   return info;
}


Now this is still a nasty, nasty way of doing things because it relies on whatever is using "info" to know which values mean what thing. Ideally you want to declare a class which explicitly says "these are the experience points", "these are the hits".

Code:
public class HitData
{
public int experience;
public int hits;
};


Or something similar. Syntax isn't checked, but hopefully this will point you in the right direction!
flatliner
Thanks very much AftershockVibe for taking the time to reply but I actually sorted my problem using pointers and references, but then i found out that it wasnt a good idea returning an array in my stituation, in terms of OO design.

Thanks very much Very Happy
snowboardalliance
flatliner wrote:
Thanks very much AftershockVibe for taking the time to reply but I actually sorted my problem using pointers and references, but then i found out that it wasnt a good idea returning an array in my stituation, in terms of OO design.

Thanks very much Very Happy


Keep in mind that in the above code, someone is responsible for cleaning up the vector. You need a delete for the new. This can get tricky because as is, you will probably quickly find memory leaks as the program gets more complex. If you are using OOP, the function could be entirely different. You could have it more like:

Code:
playerAction = player1.defencesiveAttack()

becomes
Code:
player1.defencesiveAttack(playerAction)

Have the playerAction an object of a class and have the defencesiveAttack function take it as a reference and call members to set the two variables. Sorry if I'm saying this wrong, I haven't done any c++ in almost a year.
Related topics
script backup database
GD-library header probs
Site Nav Suggestions...
A ActionScript,showing a running date.
Random Quotes Services for anyone - 1 Frih$ for 170+ quotes
subject javascript code please help
phpbb cash mod problemos
php HELP!!!
Need a script
An ip 2 location system in design, interesting find
fatal error in Joomla installation "index.php"
[code] Float para las imágenes en Wordpress
fraction function
gis application dev. for rain,river,flood
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.