Static member initialization

A

Agoston Bejo

Hello there,
is it possible to initialize such a static member that need some algorithm
for

initializing? What I mean is:

----------------------------------
Example: Platform VC++7.1
----------------------------------
#include <iostream>

using namespace std;

struct A {
static int ia[10];
}

for(int i=0;i<10;++i) { A::ia = i; } // SYNTAX ERROR: 'for'

int _tmain(int argc, _TCHAR* argv[])
{
cout << A::ia[5] << endl;
return 0;
}

---------------------------------

What is actually missing is a kind of "static constructor" for a class, sort
of

like in Java.


Any ways to do this?

Thx,
Agoston
 
V

Victor Bazarov

Agoston said:
is it possible to initialize such a static member that need some algorithm
for

initializing? What I mean is:

----------------------------------
Example: Platform VC++7.1
----------------------------------
#include <iostream>

using namespace std;

struct A {
static int ia[10];
}

for(int i=0;i<10;++i) { A::ia = i; } // SYNTAX ERROR: 'for'


There can be no stand-alone executable statement.
int _tmain(int argc, _TCHAR* argv[])
{
cout << A::ia[5] << endl;
return 0;
}

---------------------------------

What is actually missing is a kind of "static constructor" for a class, sort
of

like in Java.

You could search the archives for "static constructor", couldn't you?
Any ways to do this?

You need to introduce a special initialiser object and pass the array to
it to be filled up:

struct A {
struct A_static_initialiser {
A_static_initialiser(int (&ia)[10]) {
/* your algorithm */ ;
}
};
static int ia[10];
static A_static_initialiser asi;
};

int A::ia[10];
A::A_static_initialiser asi(A::ia); // will fill up the array

If you do this often, you could try writing a macro for that...

Victor
 
P

Pete Becker

Agoston said:
Hello there,
is it possible to initialize such a static member that need some algorithm
for

initializing? What I mean is:

----------------------------------
Example: Platform VC++7.1
----------------------------------
#include <iostream>

using namespace std;

struct A {
static int ia[10];
}

for(int i=0;i<10;++i) { A::ia = i; } // SYNTAX ERROR: 'for'


Instead of an array use a vector, which has a constructor.
 
V

Victor Bazarov

Pete said:
Agoston said:
Hello there,
is it possible to initialize such a static member that need some
algorithm for

initializing? What I mean is:

----------------------------------
Example: Platform VC++7.1
----------------------------------
#include <iostream>

using namespace std;

struct A {
static int ia[10];
}

for(int i=0;i<10;++i) { A::ia = i; } // SYNTAX ERROR: 'for'


Instead of an array use a vector, which has a constructor.


Could you please show how an arbitrary algorithm would be used to
initialise it? I mean for the OP's benefit. Thanks.

V
 
P

Pete Becker

Victor said:
Could you please show how an arbitrary algorithm would be used to
initialise it?

Nope. <g> The approach depends on the data type and the desired
initialization. Constructors are the mechanism for initializing data,
which means that things like C-style arrays are tricky to initialize,
because they don't have constructors.

Well, okay, I'll give a hint for vector: to initilize a vector with a
sequence of values you use a pair of iterators that designate the
sequence. In their simplest form the iterators simply designate the
beginning and end of some other statically initialized data structure.
For more complex initialization the first iterator of the pair can
generate data as needed.
I mean for the OP's benefit.

I think people benefit more from figuring things out for themselves.
Sometimes they need hints.
 

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
474,298
Messages
2,571,540
Members
48,275
Latest member
tetedenuit01

Latest Threads

Top