q: static variable initiaization

L

laniik

hi i have a question. i have a class

class c {
static int vector[256];
c();
void foo();
};

I want to initialize the values in that vector so that in every
instance of c, foo can use it. how do i do this? thanks!


oliver
 
V

Victor Bazarov

laniik said:
hi i have a question. i have a class

class c {
static int vector[256];
c();
void foo();
};

I want to initialize the values in that vector so that in every
instance of c, foo can use it. how do i do this? thanks!

To what do you need to initialise it?

V
 
L

laniik

well in this specific case, i need to put in each index, sqrt of the
index. but lets just say i want intex 0 to be 0, and index 511 to be
511.
 
J

jsingh.oberoi

Hi

The way to initialise a static member of the class is:
After the class declaration, use:

<data_type> class_name::variable_name = value;

Soo, lets take an example of the following class with a static integer
member:

class c {
static int count;
-----------
};

int c::count = 10;

If you want to initialise it to zero, you could have even used a short
cut notation :

int c::count; (However I would still prefer it to be made equal to 0).

Soo, for your example just after the class decl., write the following
statement:

int c::vector [] = {1,2,3,4, ....};

Hope that makes some sense.
 
L

Larry I Smith

Hi

The way to initialise a static member of the class is:
After the class declaration, use:

<data_type> class_name::variable_name = value;

Soo, lets take an example of the following class with a static integer
member:

class c {
static int count;
-----------
};

int c::count = 10;

If you want to initialise it to zero, you could have even used a short
cut notation :

int c::count; (However I would still prefer it to be made equal to 0).

Soo, for your example just after the class decl., write the following
statement:

int c::vector [] = {1,2,3,4, ....};

Hope that makes some sense.

If multiple source files will be using class 'c', then the
def should be in a header and the 'vector' and methods should be
in a seperate source file. Otherwise you may get multiple
definitions of things (like 'vector'), e.g.:

--- c.h ---

class c {
// ...
};

--- c.cpp ---

#include "c.h"

int c::vector [] = {1,2,3,4, ... };

// code for 'c::...' non-inline methods follows


Regards,
Larry
 
L

laniik

ahh. ok i understand that, but how would I initialize the values of the
vector in a loop? can i have a for loop just in the general context of
the c file?

i need to have a for loop that fills the spots because it is about 1024
in length so doing it by hand would be rediculous.

thanks!
 
L

Larry I Smith

laniik said:
ahh. ok i understand that, but how would I initialize the values of the
vector in a loop? can i have a for loop just in the general context of
the c file?

i need to have a for loop that fills the spots because it is about 1024
in length so doing it by hand would be rediculous.

thanks!

Well, there are many ways to attack this. Whatever
I recommend, someone else will suggest another approach.

ONE approach is to have a static function in your class
whose sole function is to init the shared array. Then
in your main(), explicitly call that static function
before any instances of your class are created. This
won't work if instances of the class are created before
main() is invoked (if you have global instances of your
class that are constructed before main()).

Another approach is to have a member function in your
class that is called by ALL of the class constructors.
This member function would init the array. In this
approach the member function that inits the array must
use some serialization mechanism to ensure that the
array is init'ed only once (mutex, semaphore, etc) and
that concurrent calls to it from multiple threads do
not mangle the array.

In any approach, you might want a boolean static member
of your class that denotes whether or not the array has
been initialized yet. The code that inits tha array
would set this static boolean to 'true'.

There are other ways to do it...

Regards,
Larry
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top