static constructor alike?

T

tom_usenet

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_lookup_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'?

How about this:

struct lookup_record;

class my_class {
private:
int some_internal_var;
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::lookup_table[1000];

namespace {
bool initialiser = (my_class::prepare_lookup_table(), true);
}

my_class::my_class()
{
}

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
 
J

Jacek Dziedzic

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_lookup_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.
 
J

Jesper Madsen

Jacek Dziedzic said:
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_lookup_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.

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top