Hi there,
The main difference is that a 'class' is an object type and a struct is a
'value' type. When it comes to assignment and parameter passing its
important to know the difference:
Assignment
-------------
myObject o1 = new myObject()
myObject o2 = new myObject()
o1 = o2
Objects are 'reference' types and are really pointers to an address in
memory where values are stored. In the above example space is allocated for
two myObject types. o1 points to the first myObject type and o2 points to
the second myObjects type. Because objects are reference types, its not the
values held in o2 that are copied into o1 but rather the address in memory
that is pointed too by o2. Consequently, 'o1 = o2' has the effect of
pointing both o1 and o2 at the same place in memory and changes to o1 will
change o2 and vice versa. If no references remain to the memory allocated
for the original o1 object it will eventually be garbage collected.
myStruct s1 = new myStruct()
myStruct s2 = new myStruct()
s1 = s2
Structs are 'value' types and are very much like ints or floats. In the
above example space is allocated for s1 and s2. s1 is the first myStruct
type and s2 is the second myStruct type. Because structs are value types,
its not the reference for s2 that is copied into s1 but the values.
Consequently, 's1 = s2' has the effect of maintaining two seperate areas in
memory with the same values. Sometimes this is what you want and sometimes
it isn't. For example, If your struct type contains a nested object it isn't
the values of the nested object that are copied but rather the reference. In
the case of 's1 = s2', you will have two seperate blocks of memory which
contain the same values but the object reference in both will point to the
same place in memory. To find out more about avoiding this problem look for
help and shallow vs deep copying.
Parameter Passing
---------------------
When objects are passed to a method, they are passed by reference. This
means that any changes made to the object within the method will remain once
the method had been completed.
When structs are passed to a method, they are passed by value. This means
that the system makes a copy is made and changes made to the struct will not
remain when the method is complete. You can get round this by returning the
copied struct and assigning it to a variable but there is an obvious over
head in working with copies.
Summary
----------
The choice of whether to use a class or a struct is entirely down to you,
both have there place. However, because of the overheads involved in passing
by value and the added complexity of nested objects, I uses classes most of
the time. I only use structs when the offer me exactly what i need. I hope
this helps and that my explanation was too confusing and if I am wrong on
any point my apologies in advance.
Happy coding, Peter
codymanix said:
I've got a design question. I need to keep track of some variables and I am
planning to put them inside a class or struct. Basically I'm talking about
10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these classes/structs (in most cases it will be less
then 5 however)
You should *Always* use classes. structs are only needed in very rare
circumatances which isn't the case.
--
cody
[Freeware, Games and Humor]
www.deutronium.de.vu ||
www.deutronium.tk