Page 1 of 1

[Solved] Classes, structs and unions

Posted: Thu Jun 25, 2009 3:30 pm
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.

Re: Classes, structs and unions

Posted: Thu Jun 25, 2009 4:11 pm
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.

Re: Classes, structs and unions

Posted: Thu Jun 25, 2009 4:34 pm
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

Re: Classes, structs and unions

Posted: Thu Jun 25, 2009 6:07 pm
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.

Re: Classes, structs and unions

Posted: Thu Jun 25, 2009 8:02 pm
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.

Re: Classes, structs and unions

Posted: Thu Jun 25, 2009 8:16 pm
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.

Re: Classes, structs and unions

Posted: Thu Jun 25, 2009 8:25 pm
by zodiac976
Ahh well at least unions are there if I ever need them :/.

Re: Classes, structs and unions

Posted: Sun Jun 28, 2009 7:34 pm
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.