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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:46:59+00:00 2026-06-06T20:46:59+00:00

I have this C++ class that one big complicated method compute that I would

  • 0

I have this C++ class that one big complicated method compute that I would like to feed with a “compute kernel”, a method of the same class. I figure I would do something along the lines of

class test {
int classVar_ = 42;

int compute_add(int a, int b)
{
   compute(int a, int b, this->add_())
}

int compute_mult(int a, int b)
{
   compute(int a, int b, this->mult_())
}


int compute_(int a, int b, "pass in add or multiply as f()")
{
   int c=0;
   // Some complex loops {
   c += f(a,b)
   // }
   return c;
}

int add_(int a, int b){a+b+classVar_;}
int multiply_(int a, int b){a*b+classVar_;}
...

}

but I’m not sure how I would pass in add or multiply.
An alternative to this approach would be to pass in an ENUM of some sort to specify add() or multiply(), but I wanted to avoid a switch or if inside the loops.

What’s best practice here?

  • 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-06-06T20:47:00+00:00Added an answer on June 6, 2026 at 8:47 pm

    As you suspected, passing a member function pointer is acceptable practice.

    If you need to know the syntax, it is:

    int compute_(int a, int b, int (test::*f)(int,int))
    {
       int c=0;
       // Some complex loops {
       c += (this->*f)(a,b)
       // }
       return c;
    }
    

    Representing member functions using integers, and switching, introduces programmer overhead to keep things up to date when the list of available operations changes. So you don’t want that unless there’s some important reason in a particular case.

    One alternative is to make compute even more general — instead of taking a member function, write a function template that takes any callable type:

    template <typename BinaryFunction>
    int compute_(int a, int b, BinaryFunction f) {
        // body as before but `f(a,b)` instead of `(this->*f)(a,b)`
    }
    

    This more general template is great if someone wants to use it with some operator of their own invention, that isn’t a member function of test. It’s more difficult to use in the case of the member function, though, because someone needs to capture this. There are a few ways to do that — a C++11 lambda, boost::bind, or writing out a functor longhand. For example:

    template <typename BinaryFunction>
    int compute_(int a, int b, BinaryFunction f) {
        // body as before with `f(a,b)`
    }
    
    int compute_(int a, int b, int (test::*f)(int,int))
    {
        return compute_(a, b, bind_this(f, this));
    }
    

    Defining bind_this is a bit of a pain: it’s like std::bind1st except that we’d like to work with a 3-arg functor whereas bind1st only takes a binary functor. boost::bind, and std::bind in C++11, are more flexible, and will handle the extra arguments. The following will do for this case, but doesn’t work in general to bind 2-arg member functions:

    struct bind_this {
        int (test::*f)(int,int);
        test *t;
        int operator(int a, int b) const {
            return (t->*f)(a,b);
        }
        bind_this(int (test::*f)(int,int), test *t) : f(f), t(t) {}
    };
    

    In C++11 you can just use a lambda:

    int compute_(int a, int b, int (test::*f)(int,int))
    {
        return compute_(a, b, [=](int c, int d){ return (this->*f)(c,d) });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains a boolean field like this one: public class
I have a Handler class that looks like this: class Handler{ public $group; public
Assume I have a class that looks like this: class Sample { public string
I have a class that has a big method that calls on a lot
Here's the deal. I have a big class hierarchy and I have this one
I have this class that have a function to load other classes and create
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have this class that has many class members, and a lot of different
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have this small class called City that simply holds some information about a

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.