What do you do if you need to work on a number of complex objects which are needed in the main function? References are the solution to this problem.
- [Instructor] Hello, welcome to a new section of this video course, C++ references, sprite sheets and vertex arrays. In this section we will first explore the C++ references. Then we will jump into SFML vertex arrays and sprite sheets. We will also create a randomly generated scrolling background and use the background. Now we move onto the first video of this section that deals with C++ references. In this video we are going to take a look at the use of references in C++.
In section four we talked about scope, the concept that variables declared in a function are in a block of code only as scope that is can be seen or used in that function or block. Using only the C++ knowledge we have at the moment this can cause a problem. What do we do if we need to work on a number of complex objects which are needed in main? This could imply that all the code must be in main. When we pass values to a function or return values from a function, that is exactly what we are doing.
When passing or returning by value, what happens is that a copy of the value held by the variable is made and sent into the function where it is used. The significance of this is two fold. If we want the function to make a permanent change to a variable, this system is no good to us. When a copy is made to pass in as an argument, or return from the function, processing power and memory are consumed. For a simple int or even perhaps a sprite this is fairly insignificant, however, for a complex object, perhaps an entire game world the copying process will seriously affect our game's performance.
References are the solution to these two problems. A reference is a special type of variable. A reference refers to another variable. This code is a useful example to understand this. In this code we declare an initialize, a regular int called numZombies. We then declare an initialize, an int reference called rNumZombies. The reference operator and that follows the type, determines that a reference is being declared. The R prefix at the front of the reference name is optional but useful for remembering that we are dealing with a reference.
Now we have an int called NumZombies which stores the value 100 and an int reference called rNumZombies that refers to numZombies. Anything we do to numZombies can be seen through rNumZombies and anything we do to rNumZombies we are actually doing to NumZombies. Take a look at this code snippet. In this code we declared an int called score. Next, we declare an int reference called rScore that refers to score.
Remember that anything we do to score can be seen by rScore and anything we do to rScore is actually being done to score. Therefore when we increment score with this syntax, score++ the score variable now stores the value 11. In addition, if we were to output our score, it would also output 11. The next line in this code is as rScore++. Now score actually holds the value 12 because anything we do to rScore is actually done to score.
If you want to know how this works then more will be revealed in the next section when we discuss pointers but simply put, you can consider a reference as storing a place or address in the computer's memory. That place in memory is the same place where the variable it refers to stores its value. Therefore an operation on either the reference or the variable has exactly the same effect. For now it is much more important to talk more about the why of references. There are two reasons to use references and we have already mentioned them.
Let us summarize it again. Changing or reading the value of a variable or object in another function which is otherwise out of scope. Secondly, passing or returning without making a copy. Here's a code to study and we will discuss a few things on it. So, this code begins with the prototypes of two functions, add and referenceAdd. The add function takes three int variables and referenceAdd function takes two int variables and an int reference.
When the add function is called and the variables number1, number2 and answer are passed and a copy of the values is made, a new local variable to add, that is NI, N2 and A are manipulated. As a result of this answer, back in main remains at zero. When the referenceAdd function is called number1 and number2 are again passed by a value. However, answers passed by reference when the value of NI added to N2 is assigned to the reference A, what is really happening is that the value is assigned to answer back in the main function.
It is probably obvious that we would never need to actually use a reference for something this simple. It does, however, demonstrate the mechanics of passing by reference. This sample code demonstrates how a reference can be used to alter the value of a variable in once scope using code in another. As well as being extremely convenient, passing by reference is also very efficient because no copy is made. The example using a reference to an int is a bit ambiguous because as an int is so small there is no real efficiency gained.
Later in this section we will use a reference to pass an entire level layer and the efficiency gain will be significant. There is one gotcha with references. You must assign the reference to a variable at the time you create it. This means it is not completely flexible. Don't worry about this for now. We will explore references further as well as their more flexible and slightly more complicated relations, pointers in the next section, this is largely irrelevant for an int but potentially significant for a large object of a class.
We will use the exact technique when we implement the scrolling background of the Zombie Arena game. Cool. In this video we have explored the C++ references. In the next video we will take a look at the SFML vertex array class that allows us to build up a large image that can be quickly and efficiency drawn to the screen using multiple images from a single image file.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Creating the main game loop
- Drawing the game background
- Handling errors
- Manipulating C++ variables
- Randomizing numbers
- Making decisions with if, else, and switch
- Moving game objects
- Pausing and restarting games
- Adding scoring and messages
- Using class enumerations and arrays
- Adding sound effects
- Object-oriented programming in C++
- C++ references
- Texture management
- Collision detection
- Implementing the HUD
- Finishing the game