December 2001
Intermediate to advanced
416 pages
8h 16m
English
The struct mechanism allows us to introduce new value types into our application. The declaration looks exactly the same as for a class, except that we use the struct keyword—for example,
public struct matrix
{
private double[,] m_mat;
private int m_row;
private int m_col;
// ...
}
A value type stores its data directly within the object. A matrix object, for example, directly holds the m_row and m_col integer values, as well as the handle to the two-dimensional reference type array, m_mat.
When we initialize or assign one struct object with another, a deep copy is carried out; the two objects hold the same values but remain independent, unlike the shallow-copy semantics of a reference type. This both simplifies and ...
Read now
Unlock full access