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

  • Home
  • SEARCH
  • 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 48921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:21:21+00:00 2026-05-10T16:21:21+00:00

Whilst refactoring some legacy C++ code I found that I could potentially remove some

  • 0

Whilst refactoring some legacy C++ code I found that I could potentially remove some code duplication by somehow defining a variable that could point to any class method that shared the same signature. After a little digging, I found that I could do something like the following:

class MyClass { protected:     bool CaseMethod1( int abc, const std::string& str )     {         cout << 'case 1:' << str;         return true;     }      bool CaseMethod2( int abc, const std::string& str )     {         cout << 'case 2:' << str;         return true;     }      bool CaseMethod3( int abc, const std::string& str )     {         cout << 'case 3:' << str;         return true;     }  public:     bool TestSwitch( int num )     {            bool ( MyClass::*CaseMethod )( int, const std::string& );          switch ( num )         {             case 1: CaseMethod = &MyClass::CaseMethod1;                     break;             case 2: CaseMethod = &MyClass::CaseMethod2;                     break;             case 3: CaseMethod = &MyClass::CaseMethod3;                     break;         }          ...          bool res = CaseMethod( 999, 'hello world' );          ...          reurn res;     } }; 

My question is – is this the correct way to go about this? Should I consider anything that Boost has to offer?

Edit…

Ok, my mistake – I should be calling the method like so:

bool res = ( (*this).*CaseMethod )( 999, 'Hello World' ); 
  • 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. 2026-05-10T16:21:21+00:00Added an answer on May 10, 2026 at 4:21 pm

    What you have there is a pointer-to-member-function. It will solve your problem. I am surprised that your ‘TestSwitch’ function compiles, as the calling syntax is slightly different to what you might expect. It should be:

    bool res = (this->*CaseMethod)( 999, 'hello world' ); 

    However, you might find a combination of boost::function and boost::bind makes things a little easier, as you can avoid the bizarre calling syntax.

    boost::function<bool(int,std::string)> f=     boost::bind(&MyClass::CaseMethod1,this,_1,_2); 

    Of course, this will bind it to the current this pointer: you can make the this pointer of the member function an explicit third parameter if you like:

    boost::function<bool(MyClass*,int,std::string)> f=     boost::bind(&MyClass::CaseMethod1,_1,_2,_3); 

    Another alternative might be to use virtual functions and derived classes, but that might require major changes to your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sorry, don't think you can pass arguments to your Tcl… May 12, 2026 at 7:27 am
  • Editorial Team
    Editorial Team added an answer The best way is to handle that in the backend.… May 12, 2026 at 7:27 am
  • Editorial Team
    Editorial Team added an answer What do you want the grouping for? select Name, XX… May 12, 2026 at 7:27 am

Related Questions

Whilst refactoring some old code I realised that a particular header file was full
Whilst refactoring some code I came across some getter methods that returns a std::string.
Is there a way to have a 64 bit enum in C++? Whilst refactoring
Whilst analysing some legacy code with FXCop, it occurred to me is it really

Trending Tags

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

Top Members

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.