Game Development Reference
In-Depth Information
Chapter 9
Controlling Data with Access
Modifiers
C++ allows you to control how data in variables can be accessed by using different access
modifiers. There are modifiers that tell the compiler that a variable should be shared among
classes or that the program cannot change the value of the variable after it has been initialized.
Unfortunately some of the keywords supplied by C++ are reused and mean different things in
different circumstances. I'll cover the use of the static , const , mutable , and friend keywords in
C++, and their multiple uses in this chapter.
The static Keyword
The static keyword has four major uses in C++ programs:
It is used within functions to indicate that a variable should remember its value
each time the function is called.
It is used within classes to tell the compiler that each object created from the
class should share the same variable.
It is used within classes to tell the compiler that a method can be called without
having an object instance.
It is used to tell the compiler that a variable declared outside of a class or
function should have file scope rather than global scope.
These completely different uses for the static keyword make it difficult to master or even just
to remember which effect the word will have when you use it in your code. The following section
provides examples of each case to help you understand how static behaves in everyday usage.
97
 
Search WWH ::




Custom Search