CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    57

    A beginner's question on how main "should be" structured.

    After learning C++ on my own from books, I'm about to start writing code for my first project, a craps game. I've seen code where main just calls a class function and all of the code starts with that function. Like:
    Code:
    int main()
    {
        Game::Start();
        Return 0;
    }
    On the other hand, I've seen code where the primary game loop is in main and class functions are called from that loop.

    Is there an advantage of just using main to call the first class function as in the code example above, as opposed to the primary game loop being in main and calling class functions from there?

    Thanks,
    Raptor

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: A beginner's question on how main "should be" structured.

    From a runtime perspective it doesn't matter.

    If your code should be runnable on several platforms it might easier doing as in your example though. The most obvious thing I can come up with at the moment is that for a Windows application the entry function is WinMain instead of main. Having a bare minimal amount of the code in main minimize the amount of duplicated code.

    Maybe it also can have some impact on the test code depending on the test framework.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    May 2012
    Posts
    57

    Re: A beginner's question on how main "should be" structured.

    Hi S_M_A,

    If there's no compelling reason to do it either way, I guess I'll put the primary loop in main for small projects and call a class function for larger projects.

    Thanks,
    Raptor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured