Mene-Mene![]() Member Posts: 1398 From: Fort Wayne, IN, USA Registered: 10-23-2006 |
I was wondering what the benifits were of using structures, classes, or unions? I realize that structures default to public, classes to private, and unions to protected or something of the sort, but any others? ------------------ I reserve the full right to change my views/theories at any time. |
Lazarus![]() Member Posts: 1668 From: USA Registered: 06-06-2006 |
Keeping data organized and easier to access, I think. |
SSquared![]() Member Posts: 654 From: Pacific Northwest Registered: 03-22-2005 |
In regards to C++, there is no difference between structs and classes, other than what you stated. Although, you may often see structs used as pure data holders, but this is most likely due to familiarity with 'C' structs. When it comes down to what to use, if you are working in C++, I suggest using classes. Terminology and general coding practices seem to favor the word 'class' over a struct. For example, I would say "I have an instance of a class" where I've never really heard anyone say "I have an instance of a struct." It actually sounds strange to write that. A union is different from both. A union lets you choose ONE of the items as the selected data type. For example: union myUnionName You assign a value to only ONE of these as they SHARE the same portion of memory. If you put a value in myInt and ask for myString, you will get strange results. |
Mene-Mene![]() Member Posts: 1398 From: Fort Wayne, IN, USA Registered: 10-23-2006 |
I see. I think if I remember correctly, structs are only in there due to C backwards compatability. ------------------ I reserve the full right to change my views/theories at any time. |