• Advertisement

Archived

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

Returning more then one thing?

This topic is 5992 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

Hey all, I''ve been trying to understand C++ lately, and its really starting to get annoying. I''m making this game, where theres a random amount of starts placed into a 2D array, 5 x 5 and the player(s) get to remove anywhere from 1-3 stars, from any given row. The person to take the last start loses. Anyways! Thats not what I''m asking. Can a function return more than one thing? Basically what I mean, is that I have 2 variables, of iPlayerTurn (integer, which decides whose turn it is) and bComputerPlayer (boolean, which decides if player 2 is a computer or not). Now, in the games InitPlayers() function, I want basically so that it returns, telling the main game loop, whose turn it is and if player 2 is a computer or not. I do not want to make these two variables global (because I''m sure theres a way to avoid it), but I need the function to set up both of these variables and return them to the game. How can I go about this? Should I have pointers for both, and then pass those to the InitPlayers() function, which then edits whats at the address and upon returning, both of those will be the same? Did I nail this dead on or am I way off still?
"Where genius ends, madness begins."
Estauns

Share this post


Link to post
Share on other sites
Advertisement
no you cannot, however you can create a structure.


typedef struct {
int iPlayerTurn;
bool bComputerPlayer;
} RETSTRUCT;

RETSTRUCT SomeFunction(void);


if the structure is going to be large then you''re better off using pointers or references.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Yeah, I think what you said will work.

    
void Update(int *CrazyNumber1, int *CrazyNumber2){
*CrazyNumber1++;
*CrazyNumber2*=3;
}

void main(void){
int Hey1=3, Hey2=3;

Update(&Hey1, &Hey2);

//Hey1 will now equal 4 and Hey2 equals 9


}



You may also want to look into structures.

Edited by - Thrump on November 22, 2001 4:52:35 PM

Share this post


Link to post
Share on other sites
It is possible to return more than 1 type from a function. Take a look at typelist from "Modern C++ Design", or tuples from Boost library

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
Actually, a small correction to my post: It will actually dereference the value from before incrementing the pointer.


A very good reason to use parentheses to make sure it gets evaluated in the order that you want, not what you think it may be evaulated in.

Steve ''Sly'' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can use c++ STL Library :

typedef pair TPlayerInfo ;

TPlayerInfo fun(int x, bool b) {
// do something
// and modify x and b
return TPlayerInfo(x, b) ;
}

// usage of the function:
cout << fun(10,false).first << endl ; // show x
cout << fun(10,false).second << endl; // show b

//let the code be with you

Share this post


Link to post
Share on other sites
Keep in mind that it can be more efficient to use arguments as output parameters rather than returning a structure. The compiler is not guaranteed to be able to do return optimization. If it can''t, you''re stuck with a copy and you have to make sure all of the objects you''re returning can either handle a shallow copy or you have to write the return structure''s copy constructor/assignment operator.

Also bear in mind that returning values through output pararmeters is C-compatible, which for such a simple need is probably a Good Thing.

Share this post


Link to post
Share on other sites
In C++?? I think the easiest way to have a function return more than one object is to pass values to it by reference. Go to http://www.informit.com and click on "Free Library." You''ll want to check out a book called "Teach Yourself C++ in 21 Days." Check out the chapters on pointers and references. Study them. At the end of the References chapter, the book explains how to pass by reference, and how it can solve your problem.

Share this post


Link to post
Share on other sites

  • Advertisement