Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6054825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:11:47+00:00 2026-05-23T08:11:47+00:00

Using C++, I have a text mode and a GUI mode implemented using FLTK

  • 0

Using C++, I have a text mode and a GUI mode implemented using FLTK chosen by command line option and I see a lot of redundancy in the codes except for an extra parameter in the GUI case which I need to pass in the main window widget. I’m wondering if there are ways to remove the redundancy? Maybe I have some design issue completely wrong? I’d appreciate the help and please let me know if there are additional information needed. I have thought of using optional parameter, but cannot take a NULL reference.

Here’re a skeleton of the codes that are really similar (Not exact, but should be close enough to see the general structure). There may be some more if/else loops or functions nested before I have the one different call with the extra Window& parameter, but this is basically the structure, which actually continue a few levels down.

Thanks for any help!

int Game::init(){
  if (graphics){
    std::unique_ptr<Window> window = std::unique_ptr<Window>(new Window(...))
    return Fl::run();
  } else {
    play_game();
    return 0;
  }
}

void Window::init(Fl_Widget* w, void *uData){
    Window* window = (Window*) uData;
    Window->game.play_game(window);
    //Window has a private game& that is constructed to be equal to the game above.
}

void Game::play_game(){
    while(!over()){
       foo();
       bar();
    }
}

void Game::play_game(Window& window){
    while(!over()){
       foo();
       bar(window);
    }
}

void Game::bar(){
   if(!a()){
      b();
   } else {
      c();
   }
}

void Game::bar(Window& window){
   if(!a()){
      b();
   } else {
      c(window);
      window.redraw();
   }
}

A similar but different question deals with how I deal with the static function in FLTK, I have similar code somewhere that is like this:

void Game::c(){
  if(check_this()){
    do_this();
  }
}

void Game::c(Window& window){
  Fl::run();
}

static void Window::call_back(Fl_Widget* w, void* uData){
  Window* window = (Window *) uData;
  if(window->game.check_this()){
    window->do_this();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:11:47+00:00Added an answer on May 23, 2026 at 8:11 am

    Parameter is not the way to go. Subclassing Game is the way to go. Any method that needs access to the window is virtual and overridden appropriately in the window-specific subclass.

    class Game        // I hate K&R braces, sorry
    {
    public:
        enum GameType { cli, win };
        static Game &GameFactory(GameType gt)
        {
            switch (gt)
            {
            case cli: return /* ref to instance of CliGame() */;
            case win: return /* ref to instance of WinGame() */;
            }
        }
    
        virtual int launch() = 0;
        void foo();
        void bar()
        {
           if (!a()) { b(); } else { c(); }
        }
        bool a();
        void b();
        virtual void c();
        void play()
        {
            while (!over()) { foo();  bar(); }
        }
    private:
        // need some sort of static management of instance of game, how is up to you
    };
    
    class WinGame : public Game
    {
    public:
        virtual int launch()
        {
            window = std::unique_ptr<Window>(new Window(...));
            return Fl::run();    // presumably calls play_game() sometime....
        }
    protected:
        virtual void c()
        {
            // does whatever, using window *member* (not argument)
            window.redraw();
        }
    private:
        std::unique_ptr<Window> window;
    };
    
    class CliGame : public Game
    {
        virutal int launch()
        {
            play_game();
            return 0;
        }
        virtual void c()
        {
        // does whatever
        }
    };
    
    int main()
    {
        Game::GameType graphics;
    // 'graphics' gets set somehow
        Game &g = Game::GameFactory(graphics);
        int retval = g.launch();
    // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to generate a bunch of text in vim (command mode) using for
I have text input boxes. There is validation for each of the boxes using
I have text box to enter a phone number. I'm using AJAX MaskedEditExtender and
Using HTML and CSS. I have text surrounded by a border using display:inline to
I have a text file that I want to edit using Java. It has
I have a text file which I want to filter using awk. The text
I have two text fields in my view. I did it using IB. My
I'm using NHibernate 2.1.2 and Spring 1.3 I have two Text columns (blobs) in
I have some unicode text that I want to clean up using regular expressions.
I have a html text. I had encoded it in php using urlencode function.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.