0

I am writing a small game on my phone in SDL2. In main I have a while loop and basic game control conditions which are bools. I pass 'initialise', 'update', 'quit' and the 'renderer' to a game function by reference that deals with the game logic. Now it's getting more complicated I want to separate certain logic to outside of game, and to do that I have to pass the references from main, to game, to more functions outside of game. Main would pass to game, game would pass to func2, and possibly func2 needs to pass to func3.

Do the C++ standards/specification limit the use of pass by reference? You could have a chain of 10+ functions passing down quit, update, etc.

//
// extra functions here
// which break up game
// each need quit, initialise, update and renderer
// so I pass by reference
//

// void func2(&rend, &quit, &update, &initialise)

void game(SDL_Renderer *rend, bool &initialise, bool &quit, bool &update)
{
    static Table t{};

    if (initialise)
    {
        // setup game
        func2(rend, initialise, quit, update);
    }
    
    if(update)
    {
        // refresh screen
    }

    while (PollTouchEvent)
    {
        // touching screen
        // can quit here
    }
}


int main(int argc, char *argv[])
{
    // initialise SDL
    // ...

    // game stuff
    bool quit = false;
    bool update = true;
    bool initialise = true;

    while (!quit)
    {
        game(renderer, initialise, quit, update);
    }

    // quit SDL

    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    I'm a little unsure, what are 'initialise', 'update', 'quit' and the 'renderer'? Are they boolean variables? If so then you should trying using some classes, put related variables and the functions that operate on them inside a class. That way you don't have to pass by reference. – john Jan 02 '23 at 13:36
  • 1
    Please show at least the skeleton of your code structure. Like, leave out the actual "business logic" but show us the structure, who calls what etc. – Homer512 Jan 02 '23 at 13:37
  • 2
    Passing by reference doesn't make a pile of things; there's no performance difference between passing to a single function by reference or passing through a chain of functions with references all the way down. – Pete Becker Jan 02 '23 at 14:06
  • It sounds like you're passing "state booleans" around to let other functions modify them. Don't do that - leave management of the game state to the game, and make decisions based on what those other functions return to you. – molbdnilo Jan 02 '23 at 15:15

1 Answers1

1

There is no limit to the number of times you can pass a variable by reference, other than perhaps the size of the stack — i.e. the address of a variable that is passed by reference may need to be placed on the stack, and in extreme cases (read: deeply recursive functions) that might exceed the stack’s capacity. Such a problem isn’t very common, though.

Whether it’s a good approach to be passing lots of variables by reference is a different question. In particular, if you find yourself passing the same set of references to many different functions, consider creating a struct or class-object containing all of that information as member variables and just passing around a single reference to that object instead; that will be more efficient and also much easier to maintain/update as your program changes.

Also note that modern c++ compilers can often perform optimizations more effectively on arguments that are passed by-value than on arguments passed by-reference, since in the by-value case the optimizer doesn’t have to worry about the possibility of aliasing. Whether one approach or the other is faster would have to be measured on a case-by-case basis, though, as performance can depend a lot on the details of what is being done.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234