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

  • Home
  • SEARCH
  • 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 3938034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:08:15+00:00 2026-05-20T00:08:15+00:00

I have started a migration of a high energy physics algorithm written in FORTRAN

  • 0

I have started a migration of a high energy physics algorithm written in FORTRAN to an object oriented approach in C++. The FORTRAN code uses a lot of global variables all across a lot of functions.

I have simplified the global variables into a set of input variables, and a set of invariants (variables calculated once at the beginning of the algorithm and then used by all the functions).

Also, I have divided the full algorithm into three logical steps, represented by three different classes. So, in a very simple way, I have something like this:

double calculateFactor(double x, double y, double z)
{
    InvariantsTypeA invA();
    InvariantsTypeB invB();

    // they need x, y and z
    invA.CalculateValues();
    invB.CalculateValues();

    Step1 s1();
    Step2 s2();
    Step3 s3();

    // they need x, y, z, invA and invB
    return s1.Eval() + s2.Eval() + s3.Eval();
}

My problem is:

  • for doing the calculations all the InvariantsTypeX and StepX objects need the input parameters (and these are not just three).
  • the three objects s1, s2 and s3 need the data of the invA and invB objects.
  • all the classes use several other classes through composition to do their job, and all those classes also need the input and the invariants (by example, s1 has a member object theta of class ThetaMatrix that needs x, z and invB to get constructed).
  • I cannot rewrite the algorithm to reduce the global values, because it follows several high energy physics formulas, and those formulas are just like that.

Is there a good pattern to share the input parameters and the invariants to all the objects used to calculate the result?

Should I use singletons? (but the calculateFactor function is evaluated around a million of times)

Or should I pass all the required data as arguments to the objects when they are created?(but if I do that then the data will be passed everywhere in every member object of every class, creating a mess)

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-05-20T00:08:16+00:00Added an answer on May 20, 2026 at 12:08 am

    Well, in C++ the most suitable solution, given your constraints and conditions, is represented by pointers. Many developers told you to use boost::shared_ptr. Well it is not necessary, although it provides a better performance especially when considering portability and robustness to system faults.

    It is not necessary for you to bind to boost. It is true that they are not compiled and that now standardization processes will lead to c++ with boost directly integrated as a standard library, but if you do not want to use an external library you obviously can.

    So let’s go and try to solve your problem using just C++ and what it provides actually.

    You’ll probably have a main method and there, you told before, initialize all invariants elements… so you basically have constants and they can be every possible type. no need to make them constant if you want, however, in main you instantiate your invariant elements and point them for all those components requiring their usage. First in a separate file called “common_components.hpp” consider the following (I assume that you need some types for your invariant variables):

    typedef struct {
       Type1 invariant_var1;
       Type2 invariant_var2;
       ...
       TypeN invariant_varN;
    } InvariantType; // Contains the variables I need, it is a type, instantiating it will generate a set of global variables.
    typedef InvariantType* InvariantPtr; // Will point to a set of invariants
    

    In your “main.cpp” file you’ll have:

    #include "common_components.hpp"
    // Functions declaration
    int main(int, char**);
    MyType1 CalculateValues1(InvariantPtr); /* Your functions have as imput param the pointer to globals */
    MyType2 CalculateValues2(InvariantPtr); /* Your functions have as imput param the pointer to globals */
    ...
    MyType3 CalculateValuesN(InvariantPtr); /* Your functions have as imput param the pointer to globals */
    // Main implementation
    int main(int argc, char** argv) {
       InvariantType invariants = {
          value1,
          value2,
          ...
          valueN
       }; // Instantiating all invariants I need.
       InvariantPtr global = &invariants;
       // Now I have my variable global being a pointer to global.
       // Here I have to call the functions
       CalculateValue1(global);
       CalculateValue2(global);
       ...
       CalculateValueN(global);
    }
    

    If you have functions returning or using the global variable use the pointer to the struct modifying you methods’ interface. By doing so all changes will be flooded to all using thoss variables.

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

Sidebar

Related Questions

I have started working on some code left behind by previous developers, and I'm
I have just started migrating my homegrown persistence framework to JPA. Given that the
I have started using Jython as it seems to be a excellent language, and
I have started using Linq to SQL in a (bit DDD like) system which
I have started using aspects for parameter validation in our development framework. It works
I have started to play a little with Qt 4. And then I have
We have started to use spring aop for cross cutting aspects of our application
I have started testing my UI using qUnit, so I need to simulate some
We have started using Spring framework in my project. After becoming acquainted with the
I have started coding an FTP client application (for fun). I’m trying to represent

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.