473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

static object?

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
7 1895
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
> "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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
5
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2;...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.