469,591 Members | 1,723 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

static aggregate initialization inside of class

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
1 2789
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.

Similar topics

2 posts views Thread by Bo Sun | last post: by
3 posts views Thread by Tran Tuan Anh | last post: by
7 posts views Thread by Spoon | last post: by
10 posts views Thread by Pramod | last post: by
2 posts views Thread by Ranganath | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.