| 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}.
.
|
|
|
|

|
Related Articles |
|
|