Clearing a structure - syntax s = {0} question



 DEVELOP > c-Plus-Plus > Clearing a structure - syntax s = {0} question

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 22 Nov 2005 11:23:10 AM
Object: Clearing a structure - syntax s = {0} question
Hi!
Inspired by a recent thread on this list, I'd like to ask the
following:
Given this code:
struct S
{
int a;
int b;
int c;
}
...
S s = {0};
How does this work? I mean it does clear the whole struct (at least
everywhere I tried), but what language feature causes this to happen?
Also, would this safely work for this struct S2?
struct S2
{
int a;
virtual void f();
}
Thanks,
Andre
.

User: ""

Title: Re: Clearing a structure - syntax s = {0} question 22 Nov 2005 11:42:01 AM
wrote:

Hi!

Inspired by a recent thread on this list, I'd like to ask the
following:

Given this code:

struct S
{
int a;
int b;
int c;
}

...

S s = {0};

How does this work? I mean it does clear the whole struct (at least
everywhere I tried), but what language feature causes this to happen?

Structures like arrays and structs can be initialized by providing
partial data. The rest of the variable is filled with 0 (no matter
what arch or value of 0). "0" is guaranteed to be 0 for any type and
the initialization is recursive. The {0} above could be anything but
the first item would be initialized with whatever you provide. I don't
believe any other number would work for some types while the number 0
does.


Also, would this safely work for this struct S2?

struct S2
{
int a;

virtual void f();
}

It at least doesn't work over here. Having a virtual function creates
a "non-agrigate" type and this can't be initialized with {0}.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER