Eric wrote:
"Class A will always need ... " Is this an array shared between those
"few instance of class A" or is it needed by every instance? What do
you mean by "every class A" can see? Do you mean "every object of class
A can see"? Declare/define a static data member in class A. Initialise
it to what you think is needed.
class A {
...
static char array[256]; // don't forget to define it somewhere
};
This is an array that will contain a bitcounting table that every
object of class A will need access to. I thought about having a
static array in class A, but I don't know where or how I shoulld
initialize it. The class A constructor could, but it will have to
check to see if it has already been initialized to avoid from
reinitializing it. I was thinking someone may know away to initialize
it once and never again without checking. Something like a static
class/object or singleton that is created and initialized once before
it is used by class A.
The simplest thing is another static object in the class A. That object
when created will initialise the array:
class A {
...
static char array[256];
struct ArrayInitialiser {
ArrayInitialiser(char *);
};
static ArrayInitialiser init;
};
// c-tor that will initialise the array passed to it
inline A::ArrayInitialiser::ArrayInitialiser(char *array) {
// do whatever is needed to "initialise" the 'array'
}
...
// somewhere
char A::array[256];
A::ArrayInitialiser A::init(A::array);
Make sure that the 'ArrayInitialiser's constructor does what you need.
Since it's static as well, it will only be initialised (created) once.
That will ensure that your 'array' is only intialised once.
Victor