473,386 Members | 1,754 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,386 software developers and data experts.

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 2987
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bo Sun | last post by:
hi: please take a look at the following code fragment: class Test{ public: static int i; Test() {i = 33;} };
3
by: Tran Tuan Anh | last post by:
Dear all, I am new with C++ and very confused with some features. Really appreciate if you can explain to me some of stuffs below. I define a class: class A { static A* instance = 0; };
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
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
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.