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 871243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:36:41+00:00 2026-05-15T10:36:41+00:00

Suppose I have a class with 2 static functions: class CommandHandler { public: static

  • 0

Suppose I have a class with 2 static functions:

class CommandHandler
{
public:
  static void command_one(Item);
  static void command_two(Item);
};

I have a DRY problem where I have 2 functions that have the exact same code for every single line, except for the function that it calls:

void CommandOne_User()
{
  // some code A

  CommandHandler::command_one(item);

  // some code B
}

void CommandTwo_User()
{
  // some code A

  CommandHandler::command_two(item);

  // some code B
}

I would like to remove duplication, and, ideally, do something like this:

void CommandOne_User()
{
  Function func = CommandHandler::command_one();

  Refactored_CommandUser(func);
}

void CommandTwo_User()
{
 Function func = CommandHandler::command_one();

 Refactored_CommandUser(func);
}

void Refactored_CommandUser(Function func)
{
  // some code A

  func(item);
}

I have access to Qt, but not Boost. Could someone help suggest a way on how I can refactor something like 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-15T10:36:41+00:00Added an answer on May 15, 2026 at 10:36 am

    Besides the C way (passing a function pointer) or the C++ way mentioned by Jay here there is the other (modern) c++ way with boost or with a compiler with c++0x support:

    void Refactored_CommandUser( boost::function<void (Item)> f ) {
       // alternatively std::function with proper compiler support
    }
    

    With the advantage that this encapsulates a functor, and can be combined with boost::bind (or std::bind) to pass in not only free-function pointers that match the signature exactly, but also other things, like member pointers with an object:

    struct test {
       void f( Item );
    };
    void foo( Item i, std::string const & caller );
    void bar( Item i ); 
    int main() {
       test t;
       Refactored_CommandUser( boost::bind( &test::f, &t, _1 ) );
       Refactored_CommandUser( boost::bind( foo, _1, "main" ) );
       Refactored_CommandUser( bar ); // of course you can pass a function that matches directly
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 505k
  • Answers 505k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a good question and it is something my… May 16, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer It works as desired here, are you very sure the… May 16, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer use mysql_real_escape_string .. But really, don't do that instead, install… May 16, 2026 at 3:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Suppose I have two functions which look like this: public static void myFunction1(int a,
Suppose you have a private static method called Inst() which allows the class to
I have this template : template <class SourceFormat, class DestFormat, void (*convert)(DestFormat, SourceFormat)> static
Suppose i have a function template StrCompare template<typename T=NonCaseSenCompare>//NonCaseSenCompare is a user defined class
Suppose I have a simple EventArgs subclass: class MyArgs : EventArgs { } Consider
I have a class which defines a historical extraction on a database: class ExtractionConfiguration
Is there any way to make a function call only once? Suppose I have
I seem to have faced this problem many times and I wanted to ask
I have an ATL ActiveX control that raises three events (Connected, Authenticated, Disconnected) which
I am currently stuck with this problem of displaying an image using HtmlHelper class.

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.