| | |
Multiple local variable passing, questions.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 8
Reputation:
Solved Threads: 0
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:
But in this case I'm making an RPG game with a huge amount of variables, so I would have to do:
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:
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:
C++ Syntax (Toggle Plain Text)
void foo(int &myvar); {}
C++ Syntax (Toggle Plain Text)
void foo(int &myvar, int &myvar2, ...., int &myvarN); {}
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:
C++ Syntax (Toggle Plain Text)
void data() { param1 = ... param2 = ... param3 = ... .... paramN = ... result1 = param1+param2*param3 result2 = param3*param1+param2 .... resultN = paramN-paramA+paramB } void func1(); { if(foo == foo2) { 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> } int main() { func1(); data(); return 0; }
Last edited by Belthemet; Aug 14th, 2009 at 12:58 pm.
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
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." (с)
•
•
•
•
its impossible to just pass a, b and not c ?*/
Well another alternative would be to give default values to your arguments for eg:
C++ Syntax (Toggle Plain Text)
void funcname(int a, int b=0, int c=0);
Now i can just call the function in these 3 manners for eg:
C++ Syntax (Toggle Plain Text)
int val1=10, val2=20,val3=30; funcname(val1);//Only one variable. funcname (val1,val2); //THis will consider that the 3 variable is default. funcname (val1,val2,val3); //All values given in.
C++ Syntax (Toggle Plain Text)
funcname(val1,, val3)//this would be an errror
Last edited by Sky Diploma; Aug 14th, 2009 at 1:29 pm. Reason: Added quotes.
•
•
•
•
Originally Posted by Sky Diploma
The only problem would be that you are not allowed to call like the code below
cpp Syntax (Toggle Plain Text)
funcname(val1, 0, val3);//no error
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." (с)
•
•
•
•
This error is not undecidable
Go for class structure. It will definitely be a lot easier and less work,
as well as more elegant.
Here are some ideas :
Pick a class, start implementing it, and go from there.
as well as more elegant.
Here are some ideas :
C++ Syntax (Toggle Plain Text)
class Hero{}; class BigHeadMonster { }; class Items { }; class Story { }; 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?
•
•
Join Date: Aug 2009
Posts: 8
Reputation:
Solved Threads: 0
Thanks, this seems to be the best method so far.
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
•
•
•
•
Go for class structure. It will definitely be a lot easier and less work, as well as more elegant.
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?
•
•
Join Date: Aug 2009
Posts: 8
Reputation:
Solved Threads: 0
WOW. Everything described in that link could be easily done with a function.
Last edited by Belthemet; Aug 15th, 2009 at 5:40 pm.
![]() |
Similar Threads
- warning when address of pointer to local variable is passed (C)
- Problem with Returning Pointer to local variable (C++)
- Local Variable not been Initialized (Java)
- text adventure game "local variable refferenced before assignment" (Python)
- Uninitialized Local Variable help (C++)
- [Warning] address of local variable returned??? (C++)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






