Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Initialize a static array with struct elements (http://www.velocityreviews.com/forums/t621401-initialize-a-static-array-with-struct-elements.html)

 syang8 06-20-2008 07:19 PM

Initialize a static array with struct elements

If I have a

struct S
{
int a;
float b;
};

how to initialize a static array of S?

static struct S sarray[] =
{
?????
};

 Eric Pruneau 06-20-2008 07:38 PM

Re: Initialize a static array with struct elements

"syang8" <syang8@gmail.com> a écrit dans le message de news:
beb1ce12-c854-40c2-9484-930b9d7a89a8...oglegroups.com...
> If I have a
>
> struct S
> {
> int a;
> float b;
> };
>
> how to initialize a static array of S?
>
> static struct S sarray[] =
> {
> ?????
> };

Example

struct S
{
S() : a(0){} // ctor 1
explicit S(int i) : a(i){} // ctor 2
int a;
};

int main()
{
static S s1[2] = { S(1), S(2) }; // uses ctor 2
static S s2[2]; // uses ctor 1

return 0;
}

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

Eric Pruneau

 red floyd 06-20-2008 07:53 PM

Re: Initialize a static array with struct elements

syang8 wrote:
> If I have a
>
> struct S
> {
> int a;
> float b;
> };
>
> how to initialize a static array of S?
>
> static struct S sarray[] =
> {
> ?????
> };

What book are you reading that doesn't explain POD struct
initialization? Note that the "struct" in the definition
of sarray[] is a C-ism, not needed in C++.

static S sarray[] =
{
{ 1, 2.3 },
{ 4, 5.6 },
{ 7, 8.9 }
};

 syang8 06-20-2008 08:40 PM

Re: Initialize a static array with struct elements

This answers my question. Thank you.

You are right. It is a pure C quesion. I think I posted on the wrong
group.

On Jun 20, 3:53*pm, red floyd <no.spam.h...@example.com> wrote:
> syang8 wrote:
> > If I have a

>
> > struct S
> > {
> > * int a;
> > * float b;
> > };

>
> > how to initialize a static array of S?

>
> > static struct S sarray[] =
> > {
> > * ?????
> > };

>
> What book are you reading that doesn't explain POD struct
> initialization? *Note that the "struct" in the definition
> of sarray[] is a C-ism, not needed in C++.
>
> static S sarray[] =
> {
> * * { 1, 2.3 },
> * * { 4, 5.6 },
> * * { 7, 8.9 }
>
>
>
> };- Hide quoted text -
>
> - Show quoted text -

 James Kanze 06-21-2008 09:32 AM

Re: Initialize a static array with struct elements

On Jun 20, 10:40 pm, syang8 <sya...@gmail.com> wrote:
> You are right. It is a pure C quesion. I think I posted on the
> wrong group.

Not really. In C++, such object types are called POD structs,
and they are very useful because they allow static
initialization, and thus avoid order of initialization problems.
Red Floyd's comment about C-ism only concerned the extra
"struct" in the data declaration.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 All times are GMT. The time now is 09:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.