[Solved] Classes, structs and unions

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

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.
Last edited by zodiac976 on Mon Jun 29, 2009 4:11 pm, edited 1 time in total.
User avatar
RyanPridgeon
Chaos Rift Maniac
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

Post by RyanPridgeon »

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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Classes, structs and unions

Post by MarauderIIC »

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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

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.

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;
}
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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Classes, structs and unions

Post by MarauderIIC »

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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Classes, structs and unions

Post by avansc »

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.
people who develop for a computer. but embedded systems... they are still very limited.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
zodiac976
Chaos Rift Regular
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

Post by zodiac976 »

Ahh well at least unions are there if I ever need them :/.
wacko
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 12
Joined: Sat Oct 25, 2008 1:36 am

Re: Classes, structs and unions

Post by wacko »

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:

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;
}
** 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.
Post Reply