Click here to Skip to main content
15,916,449 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionUnion Pin
_80863-Apr-09 7:39
_80863-Apr-09 7:39 
AnswerRe: Union Pin
David Crow3-Apr-09 8:10
David Crow3-Apr-09 8:10 
GeneralRe: Union Pin
_80863-Apr-09 8:20
_80863-Apr-09 8:20 
AnswerRe: Union Pin
CPallini3-Apr-09 8:19
mveCPallini3-Apr-09 8:19 
GeneralRe: Union Pin
_80863-Apr-09 8:23
_80863-Apr-09 8:23 
GeneralRe: Union Pin
led mike3-Apr-09 8:27
led mike3-Apr-09 8:27 
GeneralRe: Union Pin
_80863-Apr-09 8:35
_80863-Apr-09 8:35 
AnswerRe: Union Pin
Iain Clarke, Warrior Programmer3-Apr-09 23:59
Iain Clarke, Warrior Programmer3-Apr-09 23:59 
Unions are powerful things, but until you realise that the parts share the same memory, you'll struggle. David's picture and Carlo's talk both help, I hope.

They are very powerful in their limited way. Here's a sample of my code (no real secrets here):
union __ChannelsOn
{
    BYTE    Mask;
    struct {
        BYTE    On1                 : 1;
        BYTE    On2                 : 1;
        BYTE    On3                 : 1;
        BYTE    On4                 : 1;
        BYTE    OnTOF               : 1;
        BYTE    Unused              : 1;
        BYTE    MasterOn            : 1;
        BYTE    ScanOn              : 1;
    } Bits;
} ChannelsOn;


I have some hardware that has a command I send to it to turn channels on and off. I send a byte made up of flag bits. I could say:
__ChannelsOn c;
c.Mask = 1 << 3 | 1 << 7;
SendChannels (c);

or I say:
__ChannelsOn c;
c.Mask = 0;
c.Bits.On3 = 1;
c.Bits.ScanOn = 1;
SendChannels (c);


Both do the same thing - but which is more readable?

They are also used to make the variant structure, used to talk with COM/VB.
It's equivalent to:
struct VARIANT
{
   int nType;
   union {
      int nInt;
      long lLong;
      DWORD dwDword;
      BSTR bstr;
   } Var;
};


I hope that helps a bit,

Iain.

In the process of moving to Sweden for love (awwww).
If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

QuestionWriting the interface definatinon inside the libaray in .idl file Pin
pandit843-Apr-09 4:57
pandit843-Apr-09 4:57 
AnswerRe: Writing the interface definatinon inside the libaray in .idl file Pin
Roger Stoltz3-Apr-09 5:52
Roger Stoltz3-Apr-09 5:52 
Question(cPallini) Why "Save" menu item is ALWAYS disable? Pin
Joseph Marzbani3-Apr-09 4:46
Joseph Marzbani3-Apr-09 4:46 
AnswerRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
CPallini3-Apr-09 5:27
mveCPallini3-Apr-09 5:27 
AnswerRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
led mike3-Apr-09 5:30
led mike3-Apr-09 5:30 
QuestionRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
David Crow3-Apr-09 6:34
David Crow3-Apr-09 6:34 
AnswerRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
Joseph Marzbani3-Apr-09 7:16
Joseph Marzbani3-Apr-09 7:16 
GeneralRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
David Crow3-Apr-09 8:05
David Crow3-Apr-09 8:05 
GeneralRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
led mike3-Apr-09 8:25
led mike3-Apr-09 8:25 
QuestionRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
David Crow3-Apr-09 8:28
David Crow3-Apr-09 8:28 
AnswerRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
led mike3-Apr-09 8:35
led mike3-Apr-09 8:35 
GeneralRe: (cPallini) Why "Save" menu item is ALWAYS disable? Pin
CPallini3-Apr-09 9:39
mveCPallini3-Apr-09 9:39 
Questionproblem in C++ programming Pin
myprojectme3-Apr-09 4:30
myprojectme3-Apr-09 4:30 
AnswerRe: problem in C++ programming Pin
N a v a n e e t h3-Apr-09 4:38
N a v a n e e t h3-Apr-09 4:38 
GeneralRe: problem in C++ programming Pin
myprojectme3-Apr-09 4:50
myprojectme3-Apr-09 4:50 
AnswerRe: problem in C++ programming Pin
CPallini3-Apr-09 4:52
mveCPallini3-Apr-09 4:52 
QuestionRe: problem in C++ programming Pin
David Crow3-Apr-09 6:35
David Crow3-Apr-09 6:35 

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.