473,511 Members | 16,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 19 '05 #1
2 2579
On Thu, 11 Sep 2003 14:22:33 +0000, Jacek Dziedzic
<jacek@janowo-NOSPAM-.net> wrote:
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
Jul 19 '05 #2

"Jacek Dziedzic" <jacek@janowo-NOSPAM-.net> wrote in message
news:bj**********@korweta.task.gda.pl...
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...
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5528
by: A.M | last post by:
Hi, I have i utility class contains static functions (and also members) I usually use in may applications. Can i have a static like constructor so any time the app starts, The constructor...
3
11811
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
11
3797
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...
5
3073
by: Tina | last post by:
I'm using C# 1.1.............. I add new Class item to my project. It's created as class Math I change it to public static class Math I get an error saying that "The modifier 'static' is...
12
3408
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
7
2130
by: Morgan Cheng | last post by:
In the book *Programming C#* 4th editionby Jesse Liberty, it reads "Actually, the CLR guarantees to start running the static constructor before anything else is done with your class. However, it...
8
8912
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
14
2551
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...
5
4449
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7423
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
7510
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...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
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
3225
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
1576
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.