[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

bitfield and initialization list

mc

12/15/2008 6:21:00 PM

Hi all,

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.


Thank you in advance.

Regards,

mc


5 Answers

Gert-Jan de Vos

12/15/2008 6:51:00 PM

0

On Dec 15, 7:21 pm, "mc" <mc_r...@yahoo.com> wrote:
> Hi all,
>
> 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)
>   {
>   }};

You are close, your bf struct is just another struct which can be
initialized from its constructor:

struct bitfield
{
bitfield(unsigned a, unsigned b) :
a(a), b(b)
{
}

unsigned a : 1;
unsigned b : 1;
};

class Foo
{
Foo() : bf(false, false)
{
}

bitfield bf;
};

Alternatively: make your bitfields direct members of the Foo class.

Andrey Tarasevich

12/15/2008 7:32:00 PM

0

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

Bitfields are no different from any other data members. They can be
initialized in just like any other data members. For a standalone 'bf'
object (as declared above) it can be done with an aggregate initializer

bf s = { false, true };

However, aggregate initializer cannot be used in constructor initializer
lists, which means that if in class 'Foo' you have a data member of type
'bf' (as declared above) the only option you have is to use the '()'
initializer

class Foo {
bf s;

Foo() : s() {}
};

This will set all members of 's' to '0'.

But if you want to use specific values for each bitfield in 'bf', you'll
have to provide a user-declared constructor that does what you need. And
you'll have to give your struct a real name (why would you use such a
typedef in C++ anyway)

struct bf {
unsigned a : 1;
unsigned b : 1;

bf(bool a, bool b) : a(a), b(b) {}
};

class Foo {
bf s;

Foo() : s(false, true) {}
};

--
Best regards,
Andrey Tarasevich

James Kanze

12/16/2008 10:29:00 AM

0

On Dec 15, 8:31 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> mc 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.)

Still, I'd recommend anyone not concerned with writing headers
which are shared by C and C++ to act as if this rule doesn't
exist, and to define the class in the classical C++ fashion:

struct BitFields
{
unsigned a : 1 ;
unsigned b : 1 ;
} ;

Which leads to the rest of what you said (which was right on
target).

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

Andrey Tarasevich

12/16/2008 5:07:00 PM

0

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

mc

12/16/2008 10:19:00 PM

0

"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:gi8n7k$iud$1@news.motzarella.org...
> 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

Thank you all. I'll just add a constructor and make use a class instead of
a struct (which are classes to some extent)