Click here to Skip to main content
15,910,471 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Modal dialog - Close - ESC Pin
Rajesh R Subramanian24-Jan-06 20:01
professionalRajesh R Subramanian24-Jan-06 20:01 
QuestionUnions Vs Struct. Pin
Prakash Nadar17-Jan-06 18:00
Prakash Nadar17-Jan-06 18:00 
AnswerRe: Unions Vs Struct. Pin
ky_rerun17-Jan-06 18:04
ky_rerun17-Jan-06 18:04 
GeneralRe: Unions Vs Struct. Pin
Prakash Nadar17-Jan-06 18:20
Prakash Nadar17-Jan-06 18:20 
GeneralRe: Unions Vs Struct. Pin
Prakash Nadar17-Jan-06 22:13
Prakash Nadar17-Jan-06 22:13 
AnswerRe: Unions Vs Struct. Pin
vallikumar17-Jan-06 18:33
vallikumar17-Jan-06 18:33 
GeneralRe: Unions Vs Struct. Pin
cmk17-Jan-06 19:22
cmk17-Jan-06 19:22 
AnswerRe: Unions Vs Struct. Pin
Stephen Hewitt17-Jan-06 18:37
Stephen Hewitt17-Jan-06 18:37 
I general, the purpose of a union is to save space. Say you're storing data for a fruit and you have 3 types: Apples, Oranges and Pears. The representaion of these three fruits is distinct and a fruit can only be 1 of these types. Consider a struct that embodies this design:
struct Fruit
{
     Type m_Type; // Tells us if we're and apple, orange or pear.
     Apple m_Apple;
     Orange m_Orange;
     Pear m_Pear;
};


If we using this to save our fruit data to disc only one of the three types would actually contain valid data - The space occupied by the others is wasted. We can improve on this by using a union.

struct Fruit
{
     Type m_Type; // Tells us if we're and apple, orange or pear.
     union
     {
         Apple m_Apple;
         Orange m_Orange;
         Pear m_Pear;
     } m_Data
};


With this format we don't waste as much space - the size of the union m_Data is the size of its largest member and all members occupy the same memory.

As the previous posters indicated - This saving comes at a price - Danger!


Steve
GeneralRe: Unions Vs Struct. Pin
Owner drawn17-Jan-06 18:45
Owner drawn17-Jan-06 18:45 
GeneralRe: Unions Vs Struct. Pin
Stephen Hewitt17-Jan-06 18:53
Stephen Hewitt17-Jan-06 18:53 
GeneralRe: Unions Vs Struct. Pin
Owner drawn17-Jan-06 18:58
Owner drawn17-Jan-06 18:58 
GeneralRe: Unions Vs Struct. Pin
QuickDeveloper17-Jan-06 19:02
QuickDeveloper17-Jan-06 19:02 
Questionhow to use CDC::GetCharWidth()! please give me a instance! Pin
ebinaini17-Jan-06 15:29
ebinaini17-Jan-06 15:29 
AnswerRe: how to use CDC::GetCharWidth()! please give me a instance! Pin
Stephen Hewitt17-Jan-06 16:25
Stephen Hewitt17-Jan-06 16:25 
QuestionCInternetSession could not run in a message loop? Pin
rushing17-Jan-06 15:09
rushing17-Jan-06 15:09 
AnswerRe: CInternetSession could not run in a message loop? Pin
Prakash Nadar17-Jan-06 17:56
Prakash Nadar17-Jan-06 17:56 
GeneralRe: CInternetSession could not run in a message loop? Pin
rushing17-Jan-06 21:54
rushing17-Jan-06 21:54 
GeneralRe: CInternetSession could not run in a message loop? Pin
Prakash Nadar17-Jan-06 22:36
Prakash Nadar17-Jan-06 22:36 
GeneralRe: CInternetSession could not run in a message loop? Pin
rushing18-Jan-06 3:56
rushing18-Jan-06 3:56 
GeneralRe: CInternetSession could not run in a message loop? Pin
Prakash Nadar18-Jan-06 4:18
Prakash Nadar18-Jan-06 4:18 
GeneralRe: CInternetSession could not run in a message loop? Pin
rushing18-Jan-06 15:33
rushing18-Jan-06 15:33 
GeneralRe: CInternetSession could not run in a message loop? Pin
rushing18-Jan-06 16:35
rushing18-Jan-06 16:35 
GeneralRe: CInternetSession could not run in a message loop? Pin
rushing19-Jan-06 22:14
rushing19-Jan-06 22:14 
GeneralRe: CInternetSession could not run in a message loop? Pin
Prakash Nadar19-Jan-06 22:24
Prakash Nadar19-Jan-06 22:24 
GeneralRe: CInternetSession could not run in a message loop? Pin
rushing22-Jan-06 14:35
rushing22-Jan-06 14: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.