Please share...

The Unreal Engine Developer Course – Learn C++ & Make Games

Learn and Understand C++

Over 260 video lectures in 44 hours of video!

Learn C++: Beginner to Advanced

Join thousands of successful students!

When we pass values to a function or return values from a function that is exactly what we are doing. Passing or returning a value. What happens is that a copy of the variable’s value is made, and sent into the function where it is used. Nothing happens to the actual variable itself. C++ references allow us to work around this. But why would we even want to?

What are references and why should I use them?

You can think of a variable as a storage box in the computer’s memory. When we use a variable as a function argument we are not passing the storage box but instead we are writing a note of the value contained in that storage box and passing the note to the function. Certainly, the function can find that value useful and do computations with it but nothing will happen to the storage box itself – it still holds the same value after the function returns. Often this is a good thing but sometimes it would be useful to work on the actual variable itself.

The consequences of this situation are as follows. If we want the function to make a permanent change to a variable we need make sure to return the value and then assign it. This is not always simple. Another way of working would be useful.

When a copy is made, to pass in as an argument or return from the function, it uses up processing power and slows down our game. For a simple variable or even an object of a class, this is not too much of a problem. For a large object, perhaps a level design or many objects, perhaps all the bullets in a bullet-hell style game, this can be a big problem. Another way of working seems more vital still.

  • Beginning C++ Game Programming

    Beginning C++ Game Programming

    This book is all about offering you a very fast and fun introduction to the world of game programming and C++. It will begin by teaching you the programming basics such as variables, loops, and conditions. In this book there is very little theorizing. If you want a deep discussion of all the ins and outs of C++ concepts look elsewhere. If you are starting from a knowledge of zero C++ and want to build great games- fast, take a look at this book.
    • Setup a C++/SFML/Visual Studio development environment
    • Build a fast, fun Timber clone after learning basic C++
    • Discover the fundamentals of C++ object oriented programming and build a multi-level, top-down 2D, scrolling zombie survival/shooter
    • Explore some advanced C++ concepts and make a side scrolling, co-op split-screen multiplayer platform game with directional sound, particle effects and your very own level designs!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)
    C++ for Game programmers book

    C++ For Game Programmers

    Already know the basics of C++ but want to dig much deeper in an entirely game-focussed context? C++ for Game Programmers is that book. This best selling title supplements your knowledge by putting everything into perspective from a game-development point of view. The book points out the most effective C++ practices and steers you away from the potentially dangerous ones. It describes common C++ techniques to solve specific problems most game developers face.
    • From a gaming perspective, learn about inheritance, performance, memory management, and STL to object creation, object serialization, and scripting languages
    • Explore C++ Design Patterns and high-level problem-solving constructs that are most commonly found in games
    • Discover custom Structures and algorithms, which provides a detailed hands-on discussion of using C++ to create efficient solutions to difficult problems in a highly structured manner
    Amazon USA
    Amazon UK
    Amazon CA
    Game Programming Patterns book

    Game Programming Patterns

    If you are serious about writing high quality, reusable, efficient code using C++ or indeed any other language, then inevitably you will need to seriously hone your knowledge of game programming patterns. If you don't know what game programming patterns are they are simply the solutions to design problems. If you have programmed even a basic game you will have noticed that the code gets long and hard to manage, very quickly. The more features you add the more sprawling and messy your code gets. In addition to usability/reuse-ability, our game code needs to be efficient enough for the game to run smoothly. That's what this book will do for you.
    • Learn to write a robust game loop, how to organize your entities using components, and take advantage of the CPUs cache to improve your performance
    • Discover how to organize your entities using components, and take advantage of the CPUs cache to improve your performance.
    • Never again wonder what is the "best" way to organize your game code
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)

How to use C++ references

References are the solution “other way” to which I refer. A reference is a special type of variable. A reference “refers” to another variable. An example will be useful.

In the code above we declare and initialize a regular int called numZombies. We then declare and initialize a  int reference called rNumZombies. The reference operator & 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 a  int called numZombies which stores the value 100 and a  int reference called rNumZombies that refers to numZombies.
Anything we do to numZombies can be seen through rNumZombies and anything we do with  rNumZombies,we are actually doing with  numZombies. Take a look at the next code.

In the previous code, we declare a  int called score. Next, we declare a  int reference called rScore that refers to score. Remember that anything we do to score can be seen by rScore and anything we do with  rScore is actually being done to score.
Therefore, when we increment score like this:

The score variable now stores the value 11. In addition, if we were to output rScore, it would also output 11. The next line of code is:

Now score actually holds the value 12 because anything we do with  rScore is actually being done to score.

What is happening when we use references is that the compiler is doing work behind the scenes for us. If you want to know exactly what is happening and don’t like all this magic stuff then the next tutorial on the subject of pointers will make things clearer.

Notice I said clearer and not clear. The only way to begin to fully understand all this stuff about references is to use them repeatedly. Don’t make the mistake of trying to fully understand everything before you start using them.

But simply put, you can consider a reference as storing a place/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 remember why you use references. There are two reasons to use references and here they are again.

  1. Manipulating or reading the value of a variable/object in another function which is otherwise out of scope
  2. Passing in or returning to/from a function without making a copy (and therefore more efficiently)

Just because it is not essential to fully understand references before we use them it doesn’t mean we shouldn’t try.

Making references clearer with a full example

Take a look at this completely hypothetical and pointless code that will hopefully clarify this reference stuff.

The previous code begins with the prototypes of two functions, add and referenceAdd. The add function takes three int variables and the referenceAdd function takes two int variables and a  int reference.

When the add function is called, the variables  number1, number2, and answer are passed in, a copy of the values is made and new variables local to add ( n1, 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 value. However, the variable answer is passed by reference. When the value of n1 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 way that passing by reference works.

Tell me once more why I should use references

The previous code while pointless, demonstrated how a reference can be used to alter the value of a variable in one scope using the code in another. As well as being extremely convenient, passing by reference is also very efficient because no copy is made. Soon we will make a game using references that are actually useful. It is just handy to see the theory before you use it for real.

The problem with references

Not so much a problem as a limitation. You must assign the reference to a variable at the time you create it. This means it is not completely flexible. When we look at C++ pointers in the next tutorial we will overcome this limitation.

We will use references in the next full game project coming soon.