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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:09:00+00:00 2026-05-18T02:09:00+00:00

I have a lot of legacy code that uses a function pointer as an

  • 0

I have a lot of legacy code that uses a function pointer as an argument of the form double (*f)(double). Now I have a requirement where I need to call this function from a class but function definition uses member variables. What do I do to solve this issue? For example,

void legacy_function(double (*f)(double)) { .... }

class myclass {
   double a;
   double b;
   double c;

   void mymethod(...) {
       // need to call legacy_function() such that it uses a and b with one unknown
       // a+b*x

   }    

Note that I cannot change definitions or declarations in legacy code.

I hope this is making sense. thanks for suggestions..

  • 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-18T02:09:01+00:00Added an answer on May 18, 2026 at 2:09 am

    There’s no clean way to solve this problem. It has no elegant solution within the bounds of the standard language.

    One thing you can do is to provide a global or static variable that will serve as this pointer for the intermediate callback wrapper function (see below), and write a static intermediate callback wrapper function which will delecate the call to a non-static class method

    class myclass {
      ...
      static myclass *myclass_this;
    
      double callback_wrapper(double d) {
        assert(myclass_this != NULL);
        return myclass_this->callback(d); // calls the actual implementation
      }
    };
    

    Also write the actual callback implementation in myclass

    class myclass {
      ...
      double callback(double d) {
        // do whatever you want with `a`, `b` etc.
        return /* whatever */;
      }
      ...
    };
    

    Now you can initialize myclass_this and use the intermediate callback wrapper from inside mymethod

    ...
    void mymethod(...) {
      myclass_this = this; // initilize the context
      legacy_function(&callback_wrapper);
    }
    ...
    

    All this, of course, is terribly inelegant since it relies on global or static variables and therefore is non-reentrant.

    There are alternative methods, which all happen to be non-portable and non-standard. (Read about closures and delegates).

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

Sidebar

Related Questions

I have a lot of constants that are somehow related, at some point I
We have a lot of open discussions with potential clients, and they ask frequently
we have a lot of users running in different shared and solo-owned repositories in
I have a lot of classes in the App_Code directory could this be a
I have a lot of spare intel linux servers laying around (hundreds) and want
I have a lot of buttons and by clicking on different button, different image
We have a lot of dll-libraries written in both delphi and c++ builder, and
I have spent lot of time doing research on VIM. I am Windows guy
I have a lot of assignments where I have to continually update a Makefile
We have a lot of unit tests but they aren't run every night. I

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.