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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:06:02+00:00 2026-05-28T04:06:02+00:00

The basic idea is that I have a family of classes that all do

  • 0

The basic idea is that I have a “family” of classes that all do the same identical thing, but in a slightly different way. This “family” is used in a “high performance” loop, so speed is of the essence. Further, the specific family is specified by a configuration file…

The problem is that there is tremendous code repetition in the main function here. Is there a better way to structure this so I don’t have to write HP<objx> test and test.loop(bobloblaw) three times? (In practice, this chunk of code is many more lines than 2…)

class obj1 {
public:
    double f(double x) const { return 1.; }
};

class obj2 {
public:
    double f(double x) const { return x; }
};

class obj3 {
public:
    double f(double x) const { return x*x; }
};

template <class O>
class HP {
private:
    O obj;

public:

    double loop(const vector<double>& x) {
        double s = 0.;
        for (auto i : x) s += obj.f(i);

    return s;
    }
};

int main() {
    string config = "bob";
    double result = 0;
    vector<double> bobloblaw;

    /* Read configuration file to determine which object to use. */
    if (config == "obj1") {
        HP<obj1> test;
        result = test.loop(bobloblaw);
    } else if (config == "obj2") {
        HP<obj2> test;
        result = test.loop(bobloblaw);
    } else if (config == "obj3") {
        HP<obj3> test;
        result = test.loop(bobloblaw);
    }

    return result;
}
  • 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-28T04:06:02+00:00Added an answer on May 28, 2026 at 4:06 am

    The following is untested, but should work:

    class obj1
    {
    public:
      double f(double x) const { return 1.; }
    };
    
    class obj2
    {
    public:
      double f(double x) const { return x; }
    };
    
    class obj3
    {
    public:
      double f(double x) const { return x*x; }
    };
    
    class HPbase
    {
    public:
      virtual double loop(const vector<double>&) = 0;
    };
    
    template <class O> class HP:
      public HPbase
    {
    public:
      double loop(const vector<double>& x)
      {
        double s = 0.;
        for (auto i : x)
          s += obj.f(i);
        return s;
      }
    private:
      O obj;
    };
    
    std::unordered_map<std::string, std::unique_ptr<HPbase>> decode{
      {"obj1"}, new HP<obj1>()},
      {"obj2"}, new HP<obj2>()},
      {"obj3"}, new HP<obj3>()} };
    
    int main()
    {
      string config = "bob";
      double result = 0;
      vector<double> bobloblaw;
    
      /* Read configuration file to determine which object to use. */
      result = decode[config].loop(bobloblaw);
    }
    

    Note that the only addition is a base class for HP<> and a map which replaces the if/else logic of your code.

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

Sidebar

Related Questions

I have some basic idea on how to do this task, but I'm not
the basic idea is that you have some class that has a reference type
I have a basic implementation of alpha-beta pruning but I have no idea how
From a blog: The basic idea with CSS expressions is that you will have
The basic idea is that I have a native function I want to call
I'm trying to compile some C++ files that have the same name (fileA) but
The basic idea of what we are trying to do is that we have
I have basic idea on Kilo Virtual Machine on Mobiles , I have clear
Basic question here - I have many lines of code that look something like:
Say I have a LINQ-to-XML query that generates an anonymous type like this: var

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.