static object?

Discussion in 'C++' started by Eric, Aug 6, 2004.

  1. Eric

    Eric Guest

    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?
    Eric, Aug 6, 2004
    #1
    1. Advertising

  2. 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...
    Victor Bazarov, Aug 6, 2004
    #2
    1. Advertising

  3. Eric

    Eric Guest

    > "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.
    Eric, Aug 9, 2004
    #3
  4. 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
    Victor Bazarov, Aug 9, 2004
    #4
  5. Eric

    Eric Guest

    Victor Bazarov <> 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.
    Eric, Aug 9, 2004
    #5
  6. Eric

    David Rubin Guest

    Victor Bazarov <> wrote in message news:<3NMRc.581$09.us.to.verio.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
    David Rubin, Aug 10, 2004
    #6
  7. Eric

    Eric Guest

    (David Rubin) wrote in message news:<>...
    > 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?
    Eric, Aug 10, 2004
    #7
  8. Eric

    David Rubin Guest

    (Eric) wrote in message news:<>...
    > (David Rubin) wrote in message news:<>...
    > > 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
    David Rubin, Aug 11, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Meckelein
    Replies:
    3
    Views:
    9,255
    Kevin Spencer
    Apr 14, 2004
  2. Natan

    Static vs. non-static connection

    Natan, May 24, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    7,304
    Sami Vaaraniemi
    May 26, 2004
  3. Ben
    Replies:
    3
    Views:
    411
  4. Paul W

    Static is REALLY Static!

    Paul W, May 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    363
  5. tropos
    Replies:
    3
    Views:
    447
Loading...

Share This Page