Frequently asked questions
Q) I noticed something odd in the code we have been writing. In if
statements, such as the following:
if (event.type == Event::KeyPressed)…
How does the Event
parameter passed into the pollEvent
function end up being used? After all, don’t the variables and objects only have scope in the function in which they are declared?
A) The reason is C++ references. References in C++ are variables that act as aliases for other variables. In the code under discussion, there are no explicit references. However, references are used to pass objects to functions efficiently, avoiding unnecessary copying. As the parameter of the pollEvent
function is defined as a reference, values can be assigned to the passed-in event object and those values persist in our main
function. We will understand this more when we discuss references in the next chapter.
Q) I noticed we have coded quite a few functions of the Player
class that we don’t use...