Game Programming and Development Tools

Data Management - C++ – mene-mene

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?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

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
{
char myString[24];
int myInt;
double myDouble;
} myUnion

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.

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

I reserve the full right to change my views/theories at any time.