I have a question regarding good design in C++. I have a class A, and all objects of this class use an integer array of constant values (they should share the same array, as their values are constant). The array needs to be computed (just once) before any object A.
I thought about having another class B which contains the integer array as a static member, an init() method which would fill this array according to some formula and a static boolean flag initialized (if this variable if true then the init() method would do nothing), but I’m not sure this is the best way to solve my design issue.
So my question is, what would be a good design/way to accomplish this ?
Thanks in advance.
Since the array is constant, use
const.Since the array is shared among all instances of the class, use
static.In your header file, declare it:
In A’s source file, initialize the one instance:
Edit from comment…
Or, if the array is better generated algorithmically, use a constant pointer. The usage syntax for the array would be the same.
Note that both the array and the pointer are
const.Header:
Source file: