Lets consider a struct:
struct my_struct {
char code : 4;
};
This is a POD type. If I write something like this:
my_struct object;
what will be the value od object.code? 0? Or unspecified? Does it
depend on the place of definition (global, local, member)? In my
opinion "object" will be "zero-initialized" which means object.code ==
0. But I could not find in the Standard specific rules for bit
fields...
And know a class:
class my_class {
private:
char code : 4;
};
This is not a POD type (since code is private - I hope I remebrer this
well, but if not then lets make my_class not a POD type in a way that
does not interfare with topic of this message - this surly is
possible). If I write something like this:
my_class object;
then again the same questions. And again I think that object.code == 0,
but cannot find specific rules in the Standard.
Can someone point me the rules for bit fields initialization?
Adam Badura
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
abadura (32)
|
9/25/2006 12:46:42 PM |
|
abadura ha scritto:
> Lets consider a struct:
>
> struct my_struct {
> char code : 4;
> };
>
> This is a POD type. If I write something like this:
>
> my_struct object;
>
> what will be the value od object.code? 0? Or unspecified? Does it
> depend on the place of definition (global, local, member)? In my
> opinion "object" will be "zero-initialized" which means object.code ==
> 0. But I could not find in the Standard specific rules for bit
> fields...
In this respect, bit-fields are no different from any other scalar type:
unless initialized, their value is undefined. Notice, however, that any
POD with static storage duration is always zero-initialized, regardless
of the presence or absence of bit-fields.
> And know a class:
>
> class my_class {
> private:
> char code : 4;
> };
>
> This is not a POD type (since code is private - I hope I remebrer this
> well, but if not then lets make my_class not a POD type in a way that
> does not interfare with topic of this message - this surly is
> possible). If I write something like this:
>
> my_class object;
>
> then again the same questions. And again I think that object.code == 0,
> but cannot find specific rules in the Standard.
Even if my_class is not a POD, it has a trivial default constructor. So,
although the object is default initialized by 8.5/9, the effect is the
same as above: the value is undefined if the declaration occurs at local
scope and 0 if it occurs global scope.
> Can someone point me the rules for bit fields initialization?
There's nothing special in bit fields, they are just scalar types and
covered in 8.5.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Alberto
|
9/26/2006 2:31:52 AM
|
|