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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:37:10+00:00 2026-06-13T04:37:10+00:00

In python I can pass a reference to a function into a class and

  • 0

In python I can pass a reference to a function into a class and then use it again later by
calling a class variable with the required arguments. As below:

def add_together(a, b):
    return a + b

class Function:
    def __init__(self,action,name,num_inputs=2,num_outputs=1):
        self.action      = action
        self.num_inputs  = num_inputs
        self.num_outputs = num_outputs
        self.name        = name
        self.f_type      = True

    def solve(self,*args):
        return self.action(*args)

f = Function(add_together,"'add'")
print f.action(3,5)   

I’m moving the code to c++ for speed, but not knowing much C++, I’ve had some trouble finding out how to achieve the same. See (weak) attempt below. I’m keen to keep the logic as similar as possible.

I would like to know two things,

  1. How can I pass a reference to a function into a class?
  2. The *args in c++ I believe would work through overloading but I am unsure how to load and unload the variables.

Kind of basic questions I know but I am struggling to solve with Google.

Attempt in c++

double add_together(double a, double b) {
    return a + b;
}

class Function {
public:
    Function(int ni, double act) {
        int num_inputs = ni;
        double action = act;


    }
protected:
    int num_inputs;
    double action;
};

Function f(2,add_together);
  • 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-13T04:37:11+00:00Added an answer on June 13, 2026 at 4:37 am

    Function pointers seem to be what you are looking for. The typedef at the start isn’t necessary but it does simplify the syntax.

    typedef double (*function_ptr)(double, double);
    
    double add_together(double a, double b) {
        return a + b;
    }
    
    class Function {
    public:
        Function(int ni, function_ptr act) {
            num_inputs = ni;
            action = act;
        }
    protected:
        int num_inputs;
        function_ptr action;
    };
    
    Function f(2,add_together);
    

    Also you have an error in your constructor. You redecalred the member variables num_inputs and action as local variables in the constructor. I don’t think you wanted to do that.

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

Sidebar

Related Questions

Most high-level languages (Python, Ruby, even Java) use pass-by reference. Obviously, we don't have
With pure Python functions you can pass arguments either by order (e.g. foo(1, 2,
I originally thought Python was a pure pass-by-reference language. Coming from C/C++ I can't
Possible Duplicate: Python: How do I pass a variable by reference? I want to
I'm planning to use very big numbers in Python, but wonder if Python can
In python we can use multiprocessing modules. If there is a similar library in
In Python I can use the iterkeys() method to iterate over the keys of
In Python I can use get method to get value from an dictionary without
In Python I can define a function as follows: def func(kw1=None,kw2=None,**kwargs): ... In this
How can I get a reference to the class a method was declared in?

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.