简体   繁体   中英

Split main function into multiple files C++

I am learning C++ and I am trying to make a 2D game and I would like to know if there is any way to split the main function into multiple files which would represent the different states of the game (main menu, game ..) in order to keep the file clear and readable. I don't think I want to use other functions because I want to keep getting access to all the variables in the main function. Also, I think that #include should not be used this way, which is why I am asking.

Is it possible or am I doing it wrong ? Is it a bad idea to split the main function ? Should I use functions with pointers or references even though I want to use many variables ? I don't really know how to proceed.

I would say that what is wrong is the fact that you have many variables that you want to share over such a large piece of code that you feel like the code should be split into separate source files. That is no different than having a lot of global variables accessed by various functions throughout the application - which is also generally not a good idea.

You should think about how to organize the data into objects, grouping together related items in a logical way.

Of course you can split your program into many source files. The way to do it is usually to split it into multiple functions/classes and then put those in multiple files.

You specifically ask if you can split main() into multiple files and the question is also "yes". You have multiple options:

  • Abuse the preprocessor and put chunks of main in different files and then #include the chunks inside main() (this is a really bad idea though).

  • Split the work that main() does into classes and functions and pass them the data they need to work on. If the amount of shared state is large you can put it into struct s that you pass around - or - better, rework your design to be less dependent on shared state. (You could also use global variables, but that would be a really bad solution).

Just use classes/functions and compose your program so you don't need a ton of shared variables..

I understand you want to keep everything in one function because it may look simpler and easier to understand. However, as you add more code your function will become unmanageable. And you will realise it's easier to rewrite everything from scratch than modify existing code. We've all been there. As a suggestion you should learn how functions and classes work. In fact they are here to make programs easier to write and understand.

Speaking about main(). It's a good practice to keep this function as short as possible. It should read as a high level overview of your program. All details for into other functions you call. Something like:

int main(int argc, char* argv[])
{
    parse_arguments (argc, argv);
    initialize_display (...);
    initialize_user_input (...);
    initialize_game_engine (...);

    run_game_engine (...); // <-- here is your eternal loop with a timer

    cleanup_game_engine ();
    release_user_input ();
    release_display ();
    return 0;
}

It's just an illustration. The structure is going to be different for each game. Also it's better done with classes.

The bottom line - don't keep all code in one function. Break large functions into smaller ones and keep then as isolated as possible. Use as few global variables as possible. Simple data types (int, double...) should be passed by value. Everything else (classes, structs, strings..) should be passed by reference (preferable const). As Jesper Juhl mentioned large number of variables should be combined into structs or classes. Nowadays you should not need raw pointers at all. Use them only if you have to. There are a lot of APIs which still require pointers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM