static object?

E

Eric

My program will create a few instance of class A. Class A will always
need this 256byte array that is used for bitcounting. I have a
function that fills the array. How do a right a class or object that
will only create one of these arrays that every class A can see?
 
V

Victor Bazarov

Eric said:
My program will create a few instance of class A. Class A will always
need this 256byte array that is used for bitcounting. I have a
function that fills the array. How do a right a class or object that
will only create one of these arrays that every class A can see?

"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
};

V

P.S. Are you from the Southern US? Never mind...
 
E

Eric

"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.

P.S. Are you from the Southern US? Never mind...
Yes, I am very Southern.
 
V

Victor Bazarov

Eric said:
"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
 
E

Eric

Victor Bazarov said:
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

Thanks. That looks like what I need.
 
D

David Rubin

[snip - need for static data member?]
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.

Why not put the array declaration (in an unnamed namespace) inside the
implementation file for class A? The only reason to put this sort of
thing in the class definition is for public access (not applicable
here) and inlining, which you don't seem to be doing. /david
 
E

Eric

Why not put the array declaration (in an unnamed namespace) inside the
implementation file for class A? The only reason to put this sort of
thing in the class definition is for public access (not applicable
here) and inlining, which you don't seem to be doing. /david

With it in the array declaration (static char array[256]) in the class
definition it can be inherited correct?
 
D

David Rubin

Why not put the array declaration (in an unnamed namespace) inside the
implementation file for class A? The only reason to put this sort of
thing in the class definition is for public access (not applicable
here) and inlining, which you don't seem to be doing. /david

With it in the array declaration (static char array[256]) in the class
definition it can be inherited correct?

As per Victor's suggested implementation, it is a private member, so
yours is a moot point. /david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top