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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:13:30+00:00 2026-06-15T23:13:30+00:00

I want to design a general C++ class that defines an operator() function that

  • 0

I want to design a general C++ class that defines an operator() function that returns the value of a mathematical function at a point. However, I want that class to be able to be instantiated with either an object that already has the operator() defined or with a table of values that the class will interpolate to give the value. I thought about the following approach:

class MathFunction
{
    MathFunction(Functor& function);
    MathFunction(Mat<double> interpolationValues);

    double operator()(double radius, double angle);
};

where the constructors could set a member variable and double operator() could use a switch statement to determine how to output the double I want. This seems awfully inelegant, though.

A templated version of this class could be of use, but since the two methods don’t share any code, would it be wise to do it? And how exactly would the templated version be designed?

Not sure if I’m clear. Please comment if any clarifications are needed.

EDIT: Although I chose Useless’s answer for its completeness, I want to underline the speed and clarity of dasblikenlight’s contribution. Thanks.

  • 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-15T23:13:32+00:00Added an answer on June 15, 2026 at 11:13 pm

    If MathFunction doesn’t have any common implementation, what is it for?

    If its purpose is to provide a common interface to two implementations, consider where you’ll use it:

    1. in non-templated (or non-dependent) code, where the implementation type could reasonably vary at runtime
    2. in templated code which could reasonably depend on the type of MathFunction, or in code which will always statically know which type it’s using

    In case 1, using something like the strategy pattern is reasonable. Either MathFunction can be a statically-typed wrapper dispatching to a virtual Strategy, or you can write an abstract MathFunction base class, and instantiate one of two concrete subclasses.

    dasblinkenlight already demonstrated the Strategy version, so here is the ABC equivalent (notice how it can use a factory function to hide the implementation details)

    // public header
    class MathFunction {
    public:
        virtual ~MathFunction() {}
        virtual double operator() (double radius, double angle) = 0;
    };
    
    MathFunction* make_function(Functor& function);
    MathFunction* make_function(Mat<double> interpolationValues);
    
    // implementation
    class FunctorMathFunction : public MathFunction {
        Functor& _function;
    public:
        FunctorMathFunction(Functor& function) : _function(function) {}
        virtual double operator() (double radius, double angle) {
            return _function(radius, angle);
        }
    };
    class TableMathFunction : public MathFunction {
        Mat<double> _interpolationValues;
    public:
        TableMathFunction (Mat<double> interpolationValues) 
        : _interpolationValues(interpolationValues) {}
        virtual double operator() (double radius, double angle) {
            return ...; // Use _interpolationValues to do calculation
        }
    };
    
    MathFunction* make_function(Functor& function) {
        return new FunctorMathFunction(function);
    }
    MathFunction* (Mat<double> interpolationValues) {
        return new TableMathFunction(interpolationValues);
    }
    

    In case 2, templating MathFunction is a good choice: you can either (partially-) specialize it, or use the compile-time equivalent of the Strategy pattern, which is to parameterize MathFunction on a Policy class, to which it delegates the evaluation. If performance is a concern, saving the virtual function call (and enabling in-lining) may be useful.

    template <typename EvaluationPolicy>
    class MathFunction
    {
        EvaluationPolicy eval;
    public:
        MathFunction() {}
        MathFunction(EvaluationPolicy const &ep) : eval(ep) {}
    
        double operator() (double radius, double angle) {
            return eval.calc(radius, angle);
        }
    };
    
    class FunctionPolicy
    {
        std::function<double(double,double)> func;
    public:
        FunctionPolicy(std::function<double(double,double)> f) : func(f) {}
        double calc(double r, double a) { return func(r, a); }
    };
    
    class TablePolicy
    {
        Mat<double> mat;
    public:
        TablePolicy(Mat<double> values) : mat(values) {}
        double calc(double r, double a) { return ....; }
    };
    

    The benefit of using a Policy over just writing two distinct concrete classes (FunctionMathFunction and TableMathFunction) comes in showing their relationship, in sharing any code that doesn’t depend on the policy, and in the ability to split unrelated concerns into separate policies.

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

Sidebar

Related Questions

I want to design a webpage login that when click on the login button
I want to design a page in django that has a search bar in
I often design system where I have a class in my system that has
I want to see if anyone has a better design for a class (class
I want to describe an entity that can either function normally or be put
I want design my startup screen with progress bar. But I don't how to
Am working on international project, and want design 3 major tables structure as required
I want to design a c# program which can run program a 3rd exe
We want to design a simple domain specific language for writing test scripts to
I want to design a chat view like BlackBerry messenger (unfortunately new users are

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.