static constructor alike?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacek Dziedzic

    static constructor alike?

    Still writing my 'cell' class that needs a look-up table
    to be created before any cells are accessed. Right now I have
    a static method cell::create_lo okup_table() that does the job,
    but I was wondering if there was some kind of a "static constructor"
    that would do this automatically upon the construction of the
    first cell instance? Sure I could make a normal constructor for
    'cell' that would check a flag and create the look-up table if
    it was not created already, but keeping in mind that I'd have
    arrays of 1024x1024x1024 cells, I fear that about 10^9 of such
    unnecessary checks would be a waste of run time.

    Is there a neat way of doing that? Like a 'static
    constructor'?

    tia,
    - J.

  • tom_usenet

    #2
    Re: static constructor alike?

    On Thu, 11 Sep 2003 14:22:33 +0000, Jacek Dziedzic
    <jacek@janowo-NOSPAM-.net> wrote:
    [color=blue]
    > Still writing my 'cell' class that needs a look-up table
    >to be created before any cells are accessed. Right now I have
    >a static method cell::create_lo okup_table() that does the job,
    >but I was wondering if there was some kind of a "static constructor"
    >that would do this automatically upon the construction of the
    >first cell instance? Sure I could make a normal constructor for
    >'cell' that would check a flag and create the look-up table if
    >it was not created already, but keeping in mind that I'd have
    >arrays of 1024x1024x1024 cells, I fear that about 10^9 of such
    >unnecessary checks would be a waste of run time.
    >
    > Is there a neat way of doing that? Like a 'static
    >constructor' ?[/color]

    How about this:

    struct lookup_record;

    class my_class {
    private:
    int some_internal_v ar;
    static lookup_record lookup_table[1000];
    public:
    static void prepare_lookup_ table();
    my_class();
    };

    struct lookup_record
    {
    my_class instance;
    int lookup_value;
    };



    //in source file for my_class
    lookup_record my_class::looku p_table[1000];

    namespace {
    bool initialiser = (my_class::prep are_lookup_tabl e(), true);
    }

    my_class::my_cl ass()
    {
    }

    The dynamic initialisation of initialiser is guaranteed to be done
    before calling any method defined in the same translation unit. Note
    the use of the comma operator to cause the method to be called, but
    still initialise the initialiser with a boolean.

    Tom

    Comment

    • Jesper Madsen

      #3
      Re: static constructor alike?


      "Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
      news:bjppfd$n3l $1@korweta.task .gda.pl...[color=blue]
      > Still writing my 'cell' class that needs a look-up table
      > to be created before any cells are accessed. Right now I have
      > a static method cell::create_lo okup_table() that does the job,
      > but I was wondering if there was some kind of a "static constructor"
      > that would do this automatically upon the construction of the
      > first cell instance? Sure I could make a normal constructor for
      > 'cell' that would check a flag and create the look-up table if
      > it was not created already, but keeping in mind that I'd have
      > arrays of 1024x1024x1024 cells, I fear that about 10^9 of such
      > unnecessary checks would be a waste of run time.
      >
      > Is there a neat way of doing that? Like a 'static
      > constructor'?
      >
      > tia,
      > - J.
      >[/color]

      You can try this approach,

      class Cell{
      private:
      lookup_record* getLookupTable( ){
      static lookup_record lookup_table[1000];//it will be
      constructed on first call to getLookupTable( )
      return lookup_table;
      };
      public:
      Cell(int inSomeValue){
      getLookupTable( )[0]=inSomeValue;
      }
      };

      I would recommend a std::vector instead of the array approach.. but with the
      information available... I can't recommend anything...


      Comment

      Working...