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:

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:

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:

void data() {
param1 = ...
param2 = ...
param3 = ...
....
paramN = ...

result1 = param1+param2*param3
result2 = param3*param1+param2
....
resultN = paramN-paramA+paramB
}

void func1(); {

if(foo == foo2) {
data(); /* 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 ?*/
}

int main() {
func1();
data();
return 0;
}

Recommended Answers

All 12 Replies

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:

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

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

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.

The only problem would be that you are not allowed to call like the code below

funcname(val1,, val3)//this would be an errror

I hope this helps you out.

Thank's for the info Protuberance and Sky Diploma. I will try these methods and see if I run into any more problems.

The only problem would be that you are not allowed to call like the code below

This error is not undecidable

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.

This error is not undecidable

That was just an attempt to show that there is an exception to default arguments..

Go for class structure. It will definitely be a lot easier and less work,
as well as more elegant.

Here are some ideas :

class Hero{};
class BigHeadMonster { };
class Items { };
class Story { };
Class Map { };

Pick a class, start implementing it, and go from there.

Another solution of this problem is using array.

Thanks, this seems to be the best method so far.

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 :-/

"so far I don't see a big difference between a class and a function "

WOW. Read Me

"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.

WOW. Everything described in that link could be easily done with a function.

Maybe, but with classes it's more evidently and simply for understanding.
Class - is a complex type, which can have functions and variables(and other classes) inside it.
Function - function it also in some family type, but a function does not give such flexibility as a class.

It is simpler to decide some tasks with classes and functions, what only with functions.

For example, if your task was a few complicated. If you have to passed many arguments into your function (about 1000), and these arguments was different types.
In this case, my suggestion about array is not the most effective. More effective is using class which includes all these variables, you have to pass.
That does look better?

void func(int a, float ds, int s, ..., int aaaa) //without classes

or

class MyCalculateClass; //declaration of class
...
void func(MyCalculateClass &value) //reference to a class object which contains all your variables

I think an answer is and so clear.

The difference is re-usability. Imagine creating a program thats
stores tons of information about a person. If you needed to hold
that data for a lot of people. It would save you a lot of time, by using
classes instead of regular variables and functions.

Thanks to everyone for there help! I've managed to make two optimized versions, one fully structured based on operator overloading and vectors. The second based on classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.