Game Development Reference
In-Depth Information
7.6.3 Different Kinds of Object Types
We have seen that a class is used as a blueprint for creating objects. Also, a big
difference between primitive types and classes is that variables of a primitive type
are passed by value, whereas variables of a class type are passed by reference. Next
to the class for creating objects, C# also knows another object type called a struct .
As opposed to classes, structs are also passed by value, just like the primitive types.
Structs are generally used for more basic kinds of objects. You have already used
struct objects without knowing it. For example, the Vector2 type is a struct, as is the
Color type. Consider the following declaration:
Vector2 position;
After this declaration, the memory looks something like this:
Generally, classes can be quite complicated when represented in memory. They
can contain variables of other class, struct or primitive types. Any of these types
may in turn consist of other variables, and so on.
Figure 7.3 shows an impression of what the memory could look like when we
create a new instance of a Cannon object. You can see the different kinds of objects
that are in a Cannon instance. The Vector2 objects are structs, and therefore they
are part of the Cannon instance. Texture2D is a class, so the Cannon instance contains
references to Texture2D objects. In some cases, the references point to the same object
as is the case with colorBlue and currentColor . When you create your classes, it can
sometimes be helpful to draw these kinds of diagrams to get a feeling of which
object belongs where and at which places references to these objects are stored.
7.7 What You Have Learned
In this chapter, you have learned:
Search WWH ::




Custom Search