Appendix     C

C++ Programming

C++ is a programming language which provides an almost infinite number of options for solving problems. This book uses some techniques which may not be familiar to beginner programmers. This appendix aims to cover some of the more advanced language features used.

The Friend Keyword

C++ classes can contain fields and methods which are of different scopes. These available scopes are public, private, and protected.

  • Public scope ensures that all fields and methods can be accessed from outside the class itself.
  • Private fields and methods can be accessed only from methods which are also declared in the same class.
  • Protected fields and methods are similar to private but can also be accessed by derived classes.

It is often desirable to be able to access private fields or methods from outside a class but only in restricted places. These restricted places may be other classes or functions. One method to achieve this would be to make the fields and methods public; however, this allows any class or function access. Another option is to use the friend keyword. Consider the class shown in Listing C-1.

Listing C-1.  A Simple Class

class Simple
{
private:
       void Interact();
}
 

As you can see, the Interact method is private and therefore could be accessed only from other methods inside this same class. If we had another class with which we would like to be able to access this method, we could make it a friend, as in Listing C-2.

Listing C-2.  Simple Class’ Friend

class SimpleFriend
{
public:
       void Interact(Simple* pSimple)
       {
              pSimple->Interact();
       }
}
 
class Simple
{
       friend class SimpleFriend;
 
private:
       void Interact();
}
 

Listing C-2 shows how we can use the friend keyword to provide access to private fields and data to specific classes.

Templates

C++ is a type safe language. This means that the compiler must know the expected types of all variables at compile time. Sometimes this rigid use of types can lead to lots of duplicate code just to provide the same functionality to work on different types of objects. Fortunately, C++ provides a solution to this in the form of templates.

Templates allow us to create generic code implementations and specify the specific types when we need them in the code.

Listing C-3 shows two methods to return the minimum value of two numbers.

Listing C-3.  min Functions

inline int min(int a, int b)
{
       return (a<b) ? a : b;
}
 
inline float min(float a, float b)
{
       return (a<b) ? a : b;
}
 

If we were to continue down this path, we would have to supply different versions of min for every type supported by C++. Instead, Listing C-4 shows how we can implement min using a template.

Listing C-4.  Templatized min

template<class T>
inline const T& min(const T& a, const T& b)
{
       return (a < b) ? a : b;
}
 

Here you can see that we do not specify multiple versions of the function and that the types have been replaced with T. The T comes from the first line, which tells the compiler that this method is a template and to use T in place of specific types. If you change T to be something different, you will also need to change it in the function itself. Another difference in this template is that the function parameters and the return value are passed by reference. The original int and float methods returned built-in types and therefore were simple four-byte copies. As this template may be used with classes, it’s important to pass by reference to ensure that the copy constructor is not invoked on these objects.

Now to the code which uses the template in Listing C-5.

Listing C-5.  Using Templates

min<int>(1, 2);
min<float>(1.0f, 2.0f);
 

The code here shows that we can now specify the type of min which we would like to use where and when we need it. C++ will create specific versions of min only for the types which we actually use in our code.

The Singleton Pattern

The code samples supplied with this book make use of the singleton pattern. The specific implementation of the pattern can be seen in the sample code, which can be obtained from this book’s accompanying web site at http://www.apress.com/9781430258308 .

The singleton pattern is used to provide global access to a single instance of an object throughout the code. Many people disagree with the use of global objects; however, they can provide useful features in game development. Renderers and audio managers, for example, are objects which we often need only a single instance of in game development.

By using a singleton, we can ensure that we have a single instance of these objects which we can access from anywhere in the code. Implementing singletons in a manner where we still must call new and delete on the object also means that we are in control of how and where the memory is allocated for the objects. The only global aspect of the objects is the static pointer which we use to access our instance.

The original author of the singleton class used was Scott Bilas. Scott has full source code and detailed explanations of the technique on his web site at http://scottbilas.com/publications/gem-singleton/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.204.166