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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:21:49+00:00 2026-05-23T07:21:49+00:00

I do not know if what I am asking is doable, stupid or simple.

  • 0

I do not know if what I am asking is doable, stupid or simple.
I’ve only recently started dwelling in template functions and classes, and I was wondering if the following scenario is possible:
A class which holds a function pointer to be called. The function pointer cannot be specific, but abstract, so that whenever the class’s Constructor is called, it may accept different kinds of function pointers. When the class’s execute function is called, it executes the function pointer allocated at construction, with an argument (or arguments).
Basically the abstraction is kept throughout the design, and left over the user on what function pointer and arguments to pass. The following code has not been tested, just to demonstrate what I’m trying to do:

void (*foo)(double);
void (*bar)(double,double);
void (*blah)(float);

class Instruction
{
    protected:
      double var_a;
      double var_b;
      void (*ptr2func)(double);
      void (*ptr2func)(double,double);  
    public:
      template <typename F> Instruction(F arg1, F arg2, F arg3)
      {
         Instruction::ptr2func = &arg1;
         var_a = arg2;
         var_b = arg3;
      };    
      void execute()
      {
         (*ptr2func)(var_a);
      };
};

I do not like the fact I have to keep a list inside the class of possible overloadable function pointers. How could I possibly improve the above to generalize it as much as possible so that it can work with any kind of function pointer thrown at it ?
Bear in mind, I will want to keep a container of those instantiated objects and execute each function pointer in sequence.
Thank you !
Edit: Maybe the class should be a template it’self in order to facilitate use with many different function pointers?
Edit2: I found a way around my problem just for future reference, don’t know if it’s the right one, but it works:

class Instruction
{
  protected:
    double arg1,arg2,arg3;
  public:
    virtual void execute() = 0;
};

template <class F> class MoveArm : public Instruction
{
  private:
    F (func);    
  public:
   template <typename T> 
   MoveArm(const T& f,double a, double b)
   {
     arg1 = a;
     arg2 = b;
     func = &f;
   };

   void execute()
   {
     (func)(arg1,arg2);
   };
};

However when importing functions, their function pointers need to be typedef’d:

void doIt(double a, double b)
{
   std::cout << "moving arm at: " << a << " " << b << std::endl;
};

typedef void (*ptr2func)(double,double);

int main(int argc, char **argv) {

   MoveArm<ptr2func>  arm(doIt,0.5,2.3);
   arm.execute();

   return 0;
}
  • 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-23T07:21:49+00:00Added an answer on May 23, 2026 at 7:21 am

    If you can use C++0x and variadic templates, you can achieve this by using combination of std::function, std::bind and perfect forwarding:

    #include <iostream>
    #include <functional>
    
    template <typename Result = void>
    class instruction
    {
    public:
            template <typename Func, typename... Args>
            instruction(Func func, Args&&... args)
            {
                    m_func = std::bind(func, std::forward<Args>(args)...);
            }
    
            Result execute()
            {
                    return m_func();
            }
    private:
            std::function<Result ()> m_func;
    };
    
    double add(double a, double b)
    {
            return (a + b);
    }
    
    int main()
    {
            instruction<double> test(&add, 1.5, 2.0);
            std::cout << "result: " << test.execute() << std::endl;
    }
    

    Example with output: http://ideone.com/9HYWo

    In C++ 98/03, you’d unfortunately need to overload the constructor for up-to N-paramters yourself if you need to support variable-number of arguments. You’d also use boost::function and boost::bind instead of the std:: equivalents. And then there’s also the issue of forwarding problem, so to do perfect forwarding you’d need to do an exponential amount of overloads depending on the amount of arguments you need to support. Boost has a preprocessor library that you can use to generate the required overloads without having to write all the overloads manually; but that is quite complex.

    Here’s an example of how to do it with C++98/03, assuming the functions you pass to the instruction won’t need to take the arguments by modifiable reference, to do that, you also need to have overloads for P1& p1 instead of just const P1& p1.

    #include <iostream>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>
    
    template <typename Result = void>
    class instruction
    {
    public:
            template <typename Func>
            instruction(Func func)
            {
                    m_func = func;
            }
    
            template <typename Func, typename P1>
            instruction(Func func, const P1& p1)
            {
                    m_func = boost::bind(func, p1);
            }
    
            template <typename Func, typename P1, typename P2>
            instruction(Func func, const P1& p1, const P2& p2)
            {
                    m_func = boost::bind(func, p1, p2);
            }
    
            template <typename Func, typename P1, typename P2, typename P3>
            instruction(Func func, const P1& p1, const P2& p2, const P3& p3)
            {
                    m_func = boost::bind(func, p1, p2, p3);
            }
    
            Result execute()
            {
                    return m_func();
            }
    private:
            boost::function<Result ()> m_func;
    };
    
    double add(double a, double b)
    {
            return (a + b);
    }
    
    int main()
    {
            instruction<double> test(&add, 1.5, 2.0);
            std::cout << "result: " << test.execute() << std::endl;
    }
    

    Example: http://ideone.com/iyXp1

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

Sidebar

Related Questions

I know I'm not asking this quite right, either. Please help me better form
If I had the following select, and did not know the value to use
I would like to apologize at first instant for asking some stupid question(well not
Actually, I'm not asking how to implement this functionality myself. I know it wouldn't
I dont know if what i am asking is possible or not. That is
If you do not know what Pipe Viewer is (I did not know about
I know many people who use computers every day, who do not know how
I faced a little trouble - I do not know if I can define
To recap for those .NET gurus who might not know the Java API: ConcurrentHashMap
I was asked a question in C last night and I did not know

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.