static aggregate initialization inside of class

P

Piotr Sawuk

just a quick question out of curiosity: how to initialize const arrays?

struct srat
{
long num;
ulong den;
srat(){}
}

struct collisions
{
struct collision
{
srat q;
int xy;
uint x2_c2,y2_c2;
};
const collision toggle[2];
collisions(int DeltaX, int DeltaY, int off)
: toggle({{{1,2},3,4,5},{{1,2},3,4,5}})
//error: parse error before ';' token
//error: parse error before ';' token
//At global scope:
//error: parse error at end of saved function text
{
// toggle={{{1,2},3,4,5},{{1,2},3,4,5}};
}
}


and similar error when I remove this initialization list and instead use
the initialization inside of the constructor-body. also providing an
initialization as part of the declaration of toggle doesn't work since the
array isn't supposed to be static but should actually be initialized by
arguments given to the constructor. could someone decrypt those errors?
--
Better send the eMails to netscape.net, as to
evade useless burthening of my provider's /dev/null...

before complaining because of my rudeness, read
http://www.unet.univie.ac.at/~a9702387/en/adl/liar-faq.txt
and killfile me...

P
 
J

John Harrison

Piotr said:
just a quick question out of curiosity: how to initialize const arrays?

struct srat
{
long num;
ulong den;
srat(){}
}

struct collisions
{
struct collision
{
srat q;
int xy;
uint x2_c2,y2_c2;
};
const collision toggle[2];
collisions(int DeltaX, int DeltaY, int off)
: toggle({{{1,2},3,4,5},{{1,2},3,4,5}})
//error: parse error before ';' token
//error: parse error before ';' token
//At global scope:
//error: parse error at end of saved function text
{
// toggle={{{1,2},3,4,5},{{1,2},3,4,5}};
}
}


and similar error when I remove this initialization list and instead use
the initialization inside of the constructor-body. also providing an
initialization as part of the declaration of toggle doesn't work since the
array isn't supposed to be static but should actually be initialized by
arguments given to the constructor. could someone decrypt those errors?

Arrays which are non-static class members can only be initialised with
the default constructor for the array element type. Obviously this is a
drawback of using an array inside a class, and if the array is const
then it is a fatal drawback. But if the array is const why can't you
make it static?

If for some reason you can't do this then your only choice would be to
wrap the array in another class, i.e.

struct collisions
{
struct collision
{
srat q;
int xy;
uint x2_c2,y2_c2;
};
struct collision_array
{
collision_array()
{
// initialise here
}
collision toggle[2];
}
const collision_array toggle_array;

toogle is not const so it can be assigned in the constructor, but
toogle_array is const so it can't be modified after initialisation.

john
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top