A Brief Word About Structures


Before we dive into writing games with LlamaWorks2D, which is the subject of the next chapter, there's one more topic I'd like to touch on briefly. That topic is structures.

Occasionally you will want to create a class that is so simple that it doesn't seem worth it to write member functions to get and set its values. If that's the case, you should not make it a class. Instead, you should make it a structure.

A structure is almost exactly the same as a class. A structure can have member functions, constructors, destructors, and member data just like classes. It can have public data, private data, public functions, and private functions just the same as a class. The only difference between classes and structures is the default for all class members is private while the default for structures is public.

For example, suppose I define a class like this:

 class bitmap_region {     int top,left,bottom,right; }; 


This class does not contain the keywords public or private. By default, the four data members are set to private access. That means only member functions can access them. However, there are no member functions. If I change this to a structure, the scope changes as well. Here's the same thing as a structure:

 struct bitmap_region {     int top,left,bottom,right; }; 


Factoid

The LlamaWorks2D game engine provides only a few types that are structures. They are mostly used for program initialization and a few other minor tasks. LlamaWorks2D provides far more classes than structures.


As shown here, all structures begin with the keyword struct. This structure does not contain the keywords public or private. By default, its members are public. They can be accessed by any function in the program that has a bitmap_ region variable.

In general, you should not use many structures in your program because their members are public by default. Whenever you can, it's better to use classes that keep their data private and their functions public.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net