I have code inside one function that I want to split into my own function. I want to do this with minimal modification to the source code. Below I wrote an example example where the code segment is simply "x + = y". I want to take out this code and put it in my own function. With C, I have to do this by changing y to a pointer and then working with it. But I remember reading somewhere (now I forgot) that in C ++ there is some kind of trick where I can pass a variable in such a way that the code can remain as "x + = y".
FYI: I want to make this process (and possibly change it later) as I am profiling the charts.
void main()
{
int x = 2;
int y = 5;
#if KEEP_IN_BIG_FUNC
x += y;
#else
sub_function(y,&x);
#endif
printf("x = %d\n",x);
}
void sub_function(int y,int *x)
{
*x += y;
}
source
share