0

I am fresh to C++ from Java, and am currently in the process of figuring things out. I need some advice on something that I am implementing: it's for a simulation, and will essentially be a bunch of methods for translating between real time and simulation time (e.g. one of them will allow the user to specify a duration in simulation time, and the method will return the equivalent value in real time). There will also be a single field, which will be the length of a simulated day in real time seconds.

I can think of a few ways to implement this:

  1. Everything that uses these methods (which in turn use that variable) will descend from a single superclass. So, I could use a namespace to hold the methods and make the day length variable a field of the superclass.
  2. Make the day length variable global and use a namespace to hold the methods
  3. Have a utility class, as I probably would in Java (if I had to)

What do you think is the best option? If anyone can think of a better option than the ones listed here that would be welcome, too - I'd love to learn more about C++.

false_azure
  • 1,333
  • 4
  • 25
  • 46
  • 2
    How about option `0`? Functions in a namespace, no super class, no globals. It doesn't sound like you have anything that requires complex state and invariants to be maintained. – juanchopanza Aug 10 '14 at 08:00
  • That sounds like a good plan. I just need to make sure that day length variable is the same for all the classes, which was why I was thinking of making it static/global. – false_azure Aug 10 '14 at 08:06
  • Just pass a day length value (be it an instance of a primitive or an instance of a small user defined type) to the functions that need it. – juanchopanza Aug 10 '14 at 08:15

0 Answers0