Archived

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

Returning more then one thing?

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

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?
Estauns

Share on other sites
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 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 on other sites
Thrump, be careful with dereferencing along with increments and decrements. The first line of Update should be:
  (*CrazyNumber1)++;

Otherwise it will increment first and dereference second .

[Resist Windows XP''s Invasive Production Activation Technology!]

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 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 on other sites
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 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 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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 60
• 11
• 10
• 14
• Forum Statistics

• Total Topics
632967
• Total Posts
3009560
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!