473,322 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

q: static variable initiaization

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

Jul 23 '05 #1
6 1576
laniik wrote:
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
Jul 23 '05 #2
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.

Jul 23 '05 #3
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.

Jul 23 '05 #4
js***********@gmail.com wrote:
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #5
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!

Jul 23 '05 #6
laniik wrote:
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #7

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

Similar topics

3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
9
by: AnandRaj | last post by:
Hi guys, I have a few doubts in C. 1. Why static declartions are not allowed inside structs? eg struct a { static int i; }; Throws an error ..
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
6
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
3
by: Richard K Miller | last post by:
Here's an OOP question that perplexes me. It seems PHP doesn't treat static variables correctly in child classes. <?php class ABC { public $regular_variable = "Regular variable in ABC\n";...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.