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:repare_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