By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,404 Members | 933 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,404 IT Pros & Developers. It's quick & easy.

static object?

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Eric wrote:
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...
Jul 22 '05 #2

P: n/a
> "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.
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

P: n/a
Victor Bazarov <v.********@comAcast.net> wrote in message news:

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.
Jul 22 '05 #5

P: n/a
Victor Bazarov <v.********@comAcast.net> wrote in message news:<3N***************@newsread1.dllstx09.us.to.v erio.net>...

[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
Jul 22 '05 #6

P: n/a
da********@warpmail.net (David Rubin) wrote in message news:<82*************************@posting.google.c om>...
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?
Jul 22 '05 #7

P: n/a
em*****@myrealbox.com (Eric) wrote in message news:<a6*************************@posting.google.c om>...
da********@warpmail.net (David Rubin) wrote in message news:<82*************************@posting.google.c om>...
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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.