Andrey Tarasevich
12/16/2008 5:07:00 PM
James Kanze wrote:
>>> Is it possible to initialize a bitfield with an
>>> initialization list? If yes, what is the syntax?
>
>>> typedef struct
>>> {
>>> unsigned a : 1;
>>> unsigned b : 1;
>>> } bf;
>
>>> class Foo
>>> {
>>> Foo() : bf(false, false)
>>> {
>>> }
>>> };
>>> This and other attempts were unsuccessful.
>
>> You need to state more precisely what is it your are trying to
>> do. 'bf' is just a typedef name, not a member or base name of
>> 'Foo', which means that using it in the constructor
>> initializer list the way it is used in the above example
>> simply makes no sense whatsoever.
>
> <nitpicking mode=on>
> Yes and no. For historical reasons, "If the typedef declaration
> defines an unnamed class (or enum), the first typedef-name
> declared by the declaration to be that class type (or enum type)
> is used to denote the class type (or enum type) for linkage
> purposes only. So bf is sort of a class name, and would
> certainly be legal in an initializer list, provided the compiler
> could find a corresponding constructor. (Of course, since an
> unnamed class can't have a constructor, this is going to be
> difficult.)
Good point, but apparently, you misunderstood my intent. I wasn't really
referring to the issue of using typedef-name in place of a class name
(which is indeed legal), but rather to something more trivial. In the
OP's example name 'bp' refers to one class, while the constructor
initializer list in question belongs to a completely different class -
'Foo'. Since 'bf' is mentioned in the constructor initializer list of
'Foo', it is required to be either a base class of 'Foo' or a non-static
data member of 'Foo'. Neither is the case in the OP's example, which is
why I said it makes no sense.
Of course, there's a possibility that the OP simply forgot to include
the relevant definition into his short code sample. But somehow I doubt
that deriving 'Foo' from 'bf' was his intent. And I also doubt that he
meant to declare something as unusual as a data member named 'bf' of
type named 'bf' in class 'Foo' (even though it is technically legal).
--
Best regards,
Andrey Tarasevich