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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:10:48+00:00 2026-06-17T16:10:48+00:00

I am writing a program that performs a basic set of operations, but allows

  • 0

I am writing a program that performs a basic set of operations, but allows the user to fill in the specific functions that are called (and they choose those functions before compiling). For example, my program may call a function filter(input,&output) but the user can write their own filter.

The ways I’ve read about that may solve this problem are function pointers and virtual functions. It looks like I can either do something along the lines of

int (*pt2Filter)(float,&float) = NULL;
int IIRFilter(float input, float &output);
pt2Filter=&IIRFilter;

for a function pointer. But that doesn’t let me keep track of internal states in the filter.

Or I could make a class myClass with a virtual filter function, and then the user would make an IIR class that inherits from myClass and overwrites the filter function.

class myClass
{
    virtual void Filter(float input, float &output);
    ...
};

class IIR : public myClass
{
    float stateVariable;
    virtual void Filter(float input, float &output);
}

void IIR::Filter(float input, float &output)
{ //IIR filter done here }

I guess my question is how do I call the filter function from my program without knowing that an IIR class even exists?

Or if I’m going about this completely wrong, how do I go about calling my Filter function when my goals are to 1: let the user define whatever filter they want. 2: Don’t allow the user to change my source code

Update
This may have not been as difficult as I first thought. I created a header file where the users will say which function they want the Filter class to call using the line

//User types this into "FunctionImplementations.h"
#include "IIR.h"
typedef IIR FilterImplementation;
//then I just type
#include "FunctionImplementations.h"
FilterImplementation.filter(); //Implements IIR classes filter function
  • 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-17T16:10:49+00:00Added an answer on June 17, 2026 at 4:10 pm

    There are several ways you can achieve this kind of polymorphism.

    The main question is whether you need compile-time polymorphic behavior or run-time polymorphic behavior. In the first case, the solution is usually to define a function (or class) template to perform your generic work, and have it parameterized with the type of the callable object to be invoked by your generic code for doing the customized part of the job:

    // This is how you would define your generic procedure
    template<typename F> void do_something(F f, ...) 
    { 
        ...
        f(...); 
        ... 
    }
    
    // This is how you would use it...
    void my_func(...) { ... };
    do_something(&my_func, ...); // with a function pointer
    
    do_something([] (...) { ... }, ...); // with a lambda
    
    struct my_functor { operator void () (...) { ... } };
    do_something(my_functor(), ...); // with a functor
    

    If the type of the object which defines the customized behavior is determined only at run-time, then you have two possibilities: either you use std::function<> for encapsulating your callback, or you go with the virtual function approach. I personally would prefer the former, because it does not force you to create a hierarchy of inheritance just for the purposes of achieving dynamic polymorphism.

    This is how you use a std::function<> object:

    void my_func1(int, int) { ... }
    void my_func2(int, int) { ... }
    
    std::function<void(int, int)> fxn = &my_func1;
    fxn(2, 3);
    ...
    fxn = &my_func2;
    fxn(3, 4);
    ...
    fxn = [] (int x, int y) { ... };
    fxn(4, 5)
    

    This is how you could take advantage of it for defining your generic procedure:

    void do_something(std::function<void(int, int)> f, ...)
    {
        ...
        f(3, 4);
        ...
    }
    

    At this point, you can invoke do_something() with anything that can be assigned to an std::function (i.e. any callable object with a compatible signature).

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

Sidebar

Related Questions

I'm writing a program that allows a user to perform arithmetic operations with big
I am writing a program that performs different string operations on every letter of
I'm writing a program that lets the user input 6 temperature readings, and then
I'm writing a basic UNIX program that involves processes sending messages to each other.
I'm writing a program that performs several tests on a hardware unit, and logs
I'm writing a program that performs certain actions based on the content of an
I'm writing a program that will perform matrix operations, and I'm trying to figure
So I'm writing a relatively simple program that prompts the user for a command,
I am writing a Java program that inputs a test file, performs some modifications
I'm writing a program that allows to preform the SuperResolution algorithm. In the book,

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.