Hello,
I got a program and there is a function which has a function inside and that inside function has a function etc etc. and in one of really deep functions I realize I really need to change one of the values which "lives" in the first function. How can I do this? With reference I would have to change input for all functions. By value it would be even worst hell. By pointer? I feel it could be that, but I got no real experience with pointers. I would be thankful for help.
Changing value with a pointer isn't really any different than changing with a reference. If you could show code with the exact problem we may be of more help.
void FunctionA()
{
int x = 2;
FunctionB( x );
void FunctionB( int x )
{
void FunctionC()
{
void FunctionD()
{
//Here I want to change int x;
//Actually the x in FunctionB;
}
}
}
}
void FunctionA();
void FunctionB( int x );
void FunctionC();
void FunctionD();
int main()
{
FunctionA();
}
void FunctionA()
{
int x = 2;
FunctionB( x );
}
void FunctionB( int x)
{
FunctionC(x);
}
void FunctionC(int &x)
{
FunctionD(x);
}
void FunctionD(int &x)
{
//Change the 'x' in FunctionB
x = some new value
}
I'm no expert on design, and don't mean to offend, but it's probably a bad design if you need to go through a series of functions like that that keep going deeper and deeper while changing superficial variables in the "deep" function. What is it you're trying to do exactly that is making you want/need to structure the program like that?
Edit: I mean, it's not necessarily bad design, in a game or something you might want to pass a "deltaTime" variable into multiple layers of your update function. It might be preferable in your case to have the variable that's being changed be a property of a class so you don't have to explicitly pass it in to every related function.
Yeah, I think I get this. In my game battle is a function which is made from a really many other smaller functions and what you suggest is to make a class battle instead of fuction. Yeah propobly its the best way from design view. Thanks for your advice guys :)