11

What is the meaning of colon (:) operator in "uint isWidget : 1;" in Qt? Is "uint isWidget : 1;" equivalent to "uint isWidget(1)"?

The code in Qt is

QObjectData 
{
  public:
     virtual ~QObjectData() = 0;
     QObject *q_ptr;
     QObject *parent;
     QObjectList children;

     uint isWidget : 1;
     uint pendTimer : 1;
     uint blockSig : 1;
     uint wasDeleted : 1;
     uint ownObjectName : 1;
     uint sendChildEvents : 1;
     uint receiveChildEvents : 1;
     uint inEventHandler : 1;
     uint inThreadChangeEvent : 1;
     uint hasGuards : 1; //true iff there is one or more QPointer attached to this object
     uint unused : 22;
     int postedEvents;
     QMetaObject *metaObject; // assert dynamic 
};
Zoe
  • 27,060
  • 21
  • 118
  • 148
Sulla
  • 7,631
  • 9
  • 45
  • 71
  • Given they're bitfields as described in answers below, they're probably being used as booleans, but with the compiler specifically requested to pack them all into one 32-bit word (note 10 bits used and 22 explicitly in unused). The compiler might or might not comply. Packing like this isn't mandated for bool because on most CPUs it's slower to operate on a single bit (possibly involving &-ing and |-ing values in/out), so each bool might be a given an independent char or even word.... – Tony Delroy Jan 28 '11 at 07:53
  • "What is the meaning of colon (:) operator in "uint isWidget : 1;" in Qt?" should be What is the meaning of colon (:) operator in "uint isWidget : 1;" in C++? The language is still C++, Qt is just a set of libraries :) – Frank Osterfeld Jan 28 '11 at 08:15
  • Side comment: it would be equivalent, if maybe less readable, if the last bitfield `int unused : 22;` was spelled as `int : 22` (as long as it is actually unused. Moreover, it would also be equivalent if it was not present altogether (the compiler cannot reuse those 22 bits to hold part of the `postedEvents` attribute) – David Rodríguez - dribeas Jan 28 '11 at 08:39
  • @David, Tony is saying _if_ you said `bool foo; bool bar` this may take 16 or even 64 bits, _not_ if you use the bitfield notation - i.e. this is a motivation for using bitfields, over bools. – tobyodavies Jan 28 '11 at 09:18
  • @David: from 9.6.1 "Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined." The notation allows the programmer to determine the size of the field, not necessarily the layout. Of course what you say about layout compatibility is important, and in practice compilers do tend to do what's simple and predictable just as for char, int and other fields.... – Tony Delroy Jan 28 '11 at 09:27
  • @David: btw / "int : 0" is probably sufficient to ask the compiler to skip to the next word binary - no reason for the programmer to be calculating and hardcoding 22 themselves (unless they actually want the next field aligned on a 32-byte binary even if the word size is different). – Tony Delroy Jan 28 '11 at 09:29
  • @Tony, I completely misunderstood your comment above, just as @tobyodavies noticed. :) I have thus deleted the comment as utterly non-sensical. – David Rodríguez - dribeas Jan 30 '11 at 00:34

1 Answers1

15

This is part of C struct notation - you can specify the size of an integer field in bits by using a : numBits after the property name.

I must assume that the same syntax can be used in a C++ class (i'm a C guy, but i'm sure that this is doing the same thing in C++)

tobyodavies
  • 27,347
  • 5
  • 42
  • 57
  • 2
    It's essentially the same - the differences between C and C++ are what you'd expect, e.g. bitfields can be private in C++. – MSalters Jan 28 '11 at 09:15