Click here to Skip to main content
15,914,642 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalcustomizing OPENFILENAME - "new" look Pin
peterchen3-Nov-04 10:27
peterchen3-Nov-04 10:27 
GeneralRe: customizing OPENFILENAME - "new" look Pin
Ryan Binns3-Nov-04 18:43
Ryan Binns3-Nov-04 18:43 
GeneralRe: customizing OPENFILENAME - "new" look Pin
David Crow4-Nov-04 3:56
David Crow4-Nov-04 3:56 
GeneralUnion question Pin
Steve Messer3-Nov-04 9:18
Steve Messer3-Nov-04 9:18 
GeneralRe: Union question Pin
David Crow3-Nov-04 9:38
David Crow3-Nov-04 9:38 
GeneralRe: Union question Pin
Steve Messer3-Nov-04 9:45
Steve Messer3-Nov-04 9:45 
GeneralRe: Union question Pin
Henry miller3-Nov-04 10:32
Henry miller3-Nov-04 10:32 
GeneralRe: Union question Pin
Joaquín M López Muñoz3-Nov-04 10:06
Joaquín M López Muñoz3-Nov-04 10:06 
Your question poses a number of interesting points Smile | :)

I am guessing that this is making use of Anonymous Unions.

Yes. An anonymous union is just a way to tell the compiler that the enclosed members share the same storage. For instance:
union{
  int x;
  int y;
}
 
x=100;
cout<<y; // outputs 100 !
Here, we've used an anonymous union to locally declare two variables sharing the same position in memory. Most of the time, anonymous unions are used for type definition inside a struct:
struct foo{
  union{
    int x;
    int y;
  };
};
 
foo f;
f.x=100;
cout<<f.y; // outputs 100
Also I don't understand this notation "unsigned x: 1;", what is this saying?

This is an orthogonal feature called bitfields. By adding a bitfield specification to a member, we instruct the compiler to use the indicated bits for storage instead of what it normally would take to store the member. For instance:
struct foo{
  unsigned x:2;
  unsigned y:2;
  unsigned z:2;
  unsigned w:2;
};
Here, member variables x, y, z and w will take only two bits each, and the resulting foo will only occupy one byte (alignment issues aside.) Of course, these variables will not be able to hold a value greater than what can be expressed with two bits (3) --the compiler cannot do miracles.

What do we have if we combine these two features?
struct SomeStruct{
  union{
    unsigned int field;
    struct{
      unsigned x:     1;
      unsigned y:     1;
      unsigned z:     1;
      unsigned space: 3;
    };
  };
};
By virtue of the anonymous union, field will share the same storage as x, y, y and space. And because we are using bitfields on those, the overall effect is that SomeStruct.x will refer to the first bit of SomeStruct.field, SomeStruct.y to the second bit, etc. Got it? This kind of constructs (which are very low-level and depend on non-portable issues like type alignment, size of primitive types, big- or little-endiannness, etc.) are commonly used to reproduce bit-specified data structures like those you can find in some data protocols (for instance, TCP data units), and allow the programmer to handle the data at the sub-byte level.

Finally, is this C/C++ or just for C++?

Neither Smile | :) This construct relies on what we could call an "anonymous struct":
struct{
  unsigned x:     1;
  unsigned y:     1;
  unsigned z:     1;
  unsigned space: 3;
};
Alas, anonymous structs are not standard C or C++, though MSVC admits them. Apart from this, the rest is C/C++ (though its usage would differ in minor syntactic issues.)


Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
GeneralRe: Union question Pin
Steve Messer3-Nov-04 10:18
Steve Messer3-Nov-04 10:18 
GeneralRe: Union question[modified] Pin
Antony M Kancidrowski4-Nov-04 2:24
Antony M Kancidrowski4-Nov-04 2:24 
GeneralRe: Union question Pin
Steve Messer4-Nov-04 3:42
Steve Messer4-Nov-04 3:42 
Questionis there platform-independent FillMemory function in C/C++!? Pin
clayman873-Nov-04 9:04
clayman873-Nov-04 9:04 
AnswerRe: is there platform-independent FillMemory function in C/C++!? Pin
Joaquín M López Muñoz3-Nov-04 9:15
Joaquín M López Muñoz3-Nov-04 9:15 
AnswerRe: is there platform-independent FillMemory function in C/C++!? Pin
Rick York3-Nov-04 9:15
mveRick York3-Nov-04 9:15 
GeneralRe: is there platform-independent FillMemory function in C/C++!? Pin
clayman873-Nov-04 10:13
clayman873-Nov-04 10:13 
GeneralRe: is there platform-independent FillMemory function in C/C++!? Pin
Joaquín M López Muñoz3-Nov-04 11:02
Joaquín M López Muñoz3-Nov-04 11:02 
GeneralRe: is there platform-independent FillMemory function in C/C++!? Pin
clayman873-Nov-04 10:34
clayman873-Nov-04 10:34 
GeneralRe: is there platform-independent FillMemory function in C/C++!? Pin
Joaquín M López Muñoz3-Nov-04 11:06
Joaquín M López Muñoz3-Nov-04 11:06 
QuestionThe fastest bitmap blitting software? Pin
KellyR3-Nov-04 8:55
KellyR3-Nov-04 8:55 
AnswerRe: The fastest bitmap blitting software? Pin
ThatsAlok3-Nov-04 22:23
ThatsAlok3-Nov-04 22:23 
QuestionTwo files are the same? Pin
peterchen3-Nov-04 8:14
peterchen3-Nov-04 8:14 
AnswerRe: Two files are the same? Pin
Joaquín M López Muñoz3-Nov-04 8:33
Joaquín M López Muñoz3-Nov-04 8:33 
AnswerRe: Two files are the same? Pin
David Crow3-Nov-04 9:01
David Crow3-Nov-04 9:01 
GeneralCSplitterWnd Tab Views Pin
asv3-Nov-04 7:26
asv3-Nov-04 7:26 
GeneralHELP strange thread termination Pin
Vladimir Dubovoy3-Nov-04 7:14
Vladimir Dubovoy3-Nov-04 7:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.