473,473 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1897
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.