I use classes for just about everything and I want to know
if I should be using structures more often than I do?
Also I am wondering when I can actually use a union for
once? Since the data types inside them share the same
memory location.
[Solved] Classes, structs and unions
Moderator: Coders of Rage
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
[Solved] Classes, structs and unions
Last edited by zodiac976 on Mon Jun 29, 2009 4:11 pm, edited 1 time in total.
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: Classes, structs and unions
Usually structs are just used for C programming, as they were the closest you could get to a Class in C. So it's usually best to use structs for any parts of your projects which you might want to be backwards compatible with C.
If you only use C++ then there isn't any real difference between class and struct. In fact I think that C++ compilers interpret them both the same.
Bare in mind that when coding in C, structs cannot contain functions.
If you only use C++ then there isn't any real difference between class and struct. In fact I think that C++ compilers interpret them both the same.
Bare in mind that when coding in C, structs cannot contain functions.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Classes, structs and unions
In C++, the only difference between structs and classes are that classes default "private:" and structs default "public:".
Unions... I'd have to review, honestly. I think it's sort of "you need one of five datatypes and some are longer than others" so you put them all in a union, and you need union.myChar or union.myUnsignedLongLong
Unions... I'd have to review, honestly. I think it's sort of "you need one of five datatypes and some are longer than others" so you put them all in a union, and you need union.myChar or union.myUnsignedLongLong
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: Classes, structs and unions
Yea with unions it acts like a struct that stores data types
but all of the data types use the same memory location
which means what you do to one variable changes the
other one as far as I know but I wonder when that would
come in handy as I wanted to implement one for once.
I double checked myself and unions cannot contain a
copy constructor and in turn cannot contain a string
and are also public like a structure is.
but all of the data types use the same memory location
which means what you do to one variable changes the
other one as far as I know but I wonder when that would
come in handy as I wanted to implement one for once.
Code: Select all
#include <iostream>
using namespace std;
union Union1
{
int name1;
double name2;
}myUnion;
int main()
{
myUnion.name1 = 158;
myUnion.name2 = myUnion.name1;
cout << myUnion.name1 << endl; //output is zero
cout << myUnion.name2 << endl; //output is 158
return 0;
}
copy constructor and in turn cannot contain a string
and are also public like a structure is.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Classes, structs and unions
I think the only place they're really used nowadays is in some socket libraries. Memory is plentiful enough now that people don't really need to use unions.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: Classes, structs and unions
people who develop for a computer. but embedded systems... they are still very limited.MarauderIIC wrote:I think the only place they're really used nowadays is in some socket libraries. Memory is plentiful enough now that people don't really need to use unions.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- zodiac976
- Chaos Rift Regular
- Posts: 156
- Joined: Thu Jun 18, 2009 10:03 am
- Current Project: Booklet & Text RPG
- Favorite Gaming Platforms: PC, PS3, PSP
- Programming Language of Choice: C++
- Location: AL
- Contact:
Re: Classes, structs and unions
Ahh well at least unions are there if I ever need them :/.
Re: Classes, structs and unions
I think the big thing to remember about unions is they are as big as their largest member, and all members start at same memory location. So they can be useful:
** Note is not 100% correct its just showing the use case as i omitted some code as it was copied from some place on the internet :P
the reason this code works is because the starting memory locations of sColor and color are the same so when you read in color it writes the values into the sColor.r.... etc; This can be rather useful in some cases where you know the layout of a struct or class will be the same.
Code: Select all
union uColor {
struct sColor {
unsigned char a, b, g, r;
};
unsigned int color;
};
{
uColor clr;
fread(&clr.color, sizeof(unsigned int), 1, f);
cout << "R=" << int(clr.sColor.r) << " ";
cout << "G=" << int(clr.sColor.g) << " ";
cout << "B=" << int(clr.sColor.b) << " ";
cout << "A=" << int(clr.sColor.a) << endl;
}
the reason this code works is because the starting memory locations of sColor and color are the same so when you read in color it writes the values into the sColor.r.... etc; This can be rather useful in some cases where you know the layout of a struct or class will be the same.