|
| Difference between structure and class |
 |
Mon, 31 Mar 2008 10:30:13 +000 |
Difference between structure and class
1. Structures are values types, not reference types. This means they are
stored either in the stack or inline and have the same lifetime restrictions as
the simple data types.
2. Structures don’t support inheritance.
3. There are some differences in way constructor work for structures. In
particular, the compiler always supplies a default no parameter constructor,
which you are not permitted to replace.
4. With a structure, you can specify how the fields are to be laid out in
memory.
The new operator doesn’t work in the same way as it does for class. Instead of
allocating memory on the heap, the new operator simply calls the appropriate
constructor according to the parameters passed to it, initializing all fields.
Structures follow the same rules as any other data type; everything’s must be
initialized before use. A structure is considered fully initialized when the new
operator has been called against it or when values have been individually
assigned to all its fields. On the positive side, allocating memory for
structure is very fast because this takes place inline or on the stack. Whenever
you pass a structure as a parameter or assign a structure to another structure
(A = B), the full contents of the structures are copied, whereas for a class
only reference is copied.
|
| Post Reply
|
|
|
|
|
|
|
|
|
|