By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

static aggregate initialization inside of class

P: n/a
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/~a97023...l/liar-faq.txt
and killfile me...

P
Nov 5 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Piotr Sawuk wrote:
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
Nov 5 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.