Multiple local variable passing, questions.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 8
Reputation: Belthemet is an unknown quantity at this point 
Solved Threads: 0
Belthemet Belthemet is offline Offline
Newbie Poster

Multiple local variable passing, questions.

 
0
  #1
Aug 14th, 2009
Hello.

Currently I'm making a game in C++, its not my first game (Examples of what I've made is "The game of life" in both 3d and 2d, TicTacToe with my own AI algorithms (based on the graph theory), 2D Paratrooper game), however its my first programm that requires allot of parameters to be passed from function to function.
I'm quite familiar with the structured part of C++ and some other languages, however I'm not quite advanced in the object oriented\class part of the C++ language, so this might be my problem.

First thing I would like to say, I do not want to use global parameters, unless they are global constants. In my experience they ruin the code and ruin the concept of structured programming.

Usually If passing certain parameters would be required, I would just pass them by reference:
  1. void foo(int &myvar); {}
But in this case I'm making an RPG game with a huge amount of variables, so I would have to do:
  1. void foo(int &myvar, int &myvar2, ...., int &myvarN); {}
Sometimes, I would need to pass only one or two variables, but I can't just leave all the other variables blank.

So, I assume the best way to solve this problem is using classes, however I couldn't figure out a good way to do so. If you could share some ideas or show some examples that would be great.

Here is a more precise example:
  1. void data() {
  2. param1 = ...
  3. param2 = ...
  4. param3 = ...
  5. ....
  6. paramN = ...
  7.  
  8. result1 = param1+param2*param3
  9. result2 = param3*param1+param2
  10. ....
  11. resultN = paramN-paramA+paramB
  12. }
  13.  
  14. void func1(); {
  15.  
  16. if(foo == foo2) {
  17. data(); <span class="ad_notxt"><code class="inlinecode">/* How would I pass param1, param2 but not pass any other params? Lets say data() would be data(int a, int b, int c) its impossible to just pass a, b and not c ?*/</code></span> }
  18.  
  19. int main() {
  20. func1();
  21. data();
  22. return 0;
  23. }
Last edited by Belthemet; Aug 14th, 2009 at 12:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 66
Reputation: Protuberance will become famous soon enough Protuberance will become famous soon enough 
Solved Threads: 14
Protuberance's Avatar
Protuberance Protuberance is offline Offline
Junior Poster in Training

Re: Multiple local variable passing, questions.

 
0
  #2
Aug 14th, 2009
You can use functions with the variable number of parameters.
Example of that function is a printf() function.
It define how much parameters he have by calculating '%' symbols in the first parameter.

Examples:
MSDN
http://www.codersource.net/c++_varia...functions.html
Just information
"If a problem can be decided - it is not needed to worry, and if to decide it is impossible - worrying is useless." (с)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Multiple local variable passing, questions.

 
0
  #3
Aug 14th, 2009
its impossible to just pass a, b and not c ?*/
Not necessaryly.

Well another alternative would be to give default values to your arguments for eg:

  1. void funcname(int a, int b=0, int c=0);

Now i can just call the function in these 3 manners for eg:

  1. int val1=10, val2=20,val3=30;
  2.  
  3. funcname(val1);//Only one variable.
  4. funcname (val1,val2); //THis will consider that the 3 variable is default.
  5. funcname (val1,val2,val3); //All values given in.
The only problem would be that you are not allowed to call like the code below
  1. funcname(val1,, val3)//this would be an errror
I hope this helps you out.
Last edited by Sky Diploma; Aug 14th, 2009 at 1:29 pm. Reason: Added quotes.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: Belthemet is an unknown quantity at this point 
Solved Threads: 0
Belthemet Belthemet is offline Offline
Newbie Poster

Re: Multiple local variable passing, questions.

 
0
  #4
Aug 14th, 2009
Thank's for the info Protuberance and Sky Diploma. I will try these methods and see if I run into any more problems.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 66
Reputation: Protuberance will become famous soon enough Protuberance will become famous soon enough 
Solved Threads: 14
Protuberance's Avatar
Protuberance Protuberance is offline Offline
Junior Poster in Training

Re: Multiple local variable passing, questions.

 
0
  #5
Aug 14th, 2009
Originally Posted by Sky Diploma
The only problem would be that you are not allowed to call like the code below
This error is not undecidable
  1. funcname(val1, 0, val3);//no error
Another solution of this problem is using array.
You can allocate memory for temporary array and pass it to the function. Since that function will return some result, just free memory which allocated for an array.
That method is not very good. But it's effective. And don't forget to free allocated memory, because you can get the memory-leak.

P.S. for array method - you have to pass a size of array into the function. Because otherwise you can get OutOfRangeException.

P.P.S. If you will use std::vector or something similar from STL, there is no need to pass size of array. You can define size into the function.
Last edited by Protuberance; Aug 14th, 2009 at 3:03 pm.
"If a problem can be decided - it is not needed to worry, and if to decide it is impossible - worrying is useless." (с)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Multiple local variable passing, questions.

 
0
  #6
Aug 14th, 2009
This error is not undecidable
That was just an attempt to show that there is an exception to default arguments..
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,279
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Multiple local variable passing, questions.

 
0
  #7
Aug 14th, 2009
Go for class structure. It will definitely be a lot easier and less work,
as well as more elegant.

Here are some ideas :

  1. class Hero{};
  2. class BigHeadMonster { };
  3. class Items { };
  4. class Story { };
  5. Class Map { };

Pick a class, start implementing it, and go from there.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: Belthemet is an unknown quantity at this point 
Solved Threads: 0
Belthemet Belthemet is offline Offline
Newbie Poster

Re: Multiple local variable passing, questions.

 
0
  #8
Aug 15th, 2009
Originally Posted by Protuberance View Post
Another solution of this problem is using array.
Thanks, this seems to be the best method so far.

Originally Posted by firstPerson View Post
Go for class structure. It will definitely be a lot easier and less work, as well as more elegant.
Thank's for the reply firstPerson, I have mentioned that I havent fully explored the class and OOP potential of C++. Could you show some examples/tutorials on using classes? So far I don't see a big difference between a class and a function
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,279
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 157
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Multiple local variable passing, questions.

 
0
  #9
Aug 15th, 2009
"so far I don't see a big difference between a class and a function "

WOW. Read Me
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*]
      [*solved by : murtan]
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 8
Reputation: Belthemet is an unknown quantity at this point 
Solved Threads: 0
Belthemet Belthemet is offline Offline
Newbie Poster

Re: Multiple local variable passing, questions.

 
0
  #10
Aug 15th, 2009
Originally Posted by firstPerson View Post
"so far I don't see a big difference between a class and a function "

WOW. Read Me
WOW. Everything described in that link could be easily done with a function.
Last edited by Belthemet; Aug 15th, 2009 at 5:40 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC