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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:53:56+00:00 2026-05-14T02:53:56+00:00

I was working on my advanced calculus homework today and we’re doing some iteration

  • 0

I was working on my advanced calculus homework today and we’re doing some iteration methods along the lines of newton’s method to find solutions to things like x^2=2. It got me thinking that I could write a function that would take two function pointers, one to the function itself and one to the derivative and automate the process. This wouldn’t be too challenging, then I started thinking could I have the user input a function and parse that input (yes I can do that). But can I then dynamically create a pointer to a one-variable function in c++. For instance if x^2+x, can I make a function double function(double x){ return x*x+x;} during run-time. Is this remotely feasible, or is it along the lines of self-modifying code?

Edit:

So I suppose how this could be done if you stored the information in an array and that had a function that evaluated the information stored in this array with a given input. Then you could create a class and initialize the array inside of that class and then use the function from there. Is there a better way?

  • 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-14T02:53:57+00:00Added an answer on May 14, 2026 at 2:53 am

    You can’t dynamically create a function in the sense that you can generate raw machine code for it, but you can quite easily create mathematical expressions using polymorphism:

    struct Expr
    {
      virtual double eval(double x) = 0;
    };
    
    struct Sum : Expr
    {
      Sum(Expr* a, Expr* b):a(a), b(b) {}
      virtual double eval(double x) {return a->eval(x) + b->eval(x);}
    private:
      Expr *a, *b;
    };
    
    struct Product : Expr
    {
      Product(Expr* a, Expr* b):a(a), b(b) {}
      virtual double eval(double x) {return a->eval(x) * b->eval(x);}
    private:
      Expr *a, *b;
    };
    
    struct VarX : Expr
    {
      virtual double eval(double x) {return x;}
    };
    
    struct Constant : Expr
    {
      Constant(double c):c(c) {}
      virtual double eval(double x) {return c;}
    private:
      double c;
    };
    

    You can then parse your expression into an Expr object at runtime. For example, x^2+x would be Expr* e = new Sum(new Product(new VarX(), new VarX()), new VarX()). You can then evaluate that for a given value of x by using e->eval(x).

    Note: in the above code, I have ignored const-correctness for clarity — you should not 🙂

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

Sidebar

Related Questions

I've been working with Visual Studio 2010 Beta-2 to get some advanced learning on
I'm working on some advanced browser detection, and I've downloaded the MDBF browser file
I'm working with Beta 2 of Visual Studio 2010 to get some advanced understanding
I'm working on an 'advanced search' page on a site where you would enter
I am working on a project for my advanced Java class, and the assignment
Working with python interactively, it's sometimes necessary to display a result which is some
I'm working on an advanced map application in Google Maps API V3. I'm using
I'm working on a new project, it is a wordpress theme with advanced functionality.
I am working on Advanced Search customization. Advanced Search page has a property picker
I have been working in a business writing advanced software apps, and obviously im

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.