pacman/exercises/14/parameters/README.md

38 lines
1.0 KiB
Markdown
Raw Normal View History

2021-10-17 10:51:50 +00:00
[< Back](../README.md)
2021-10-17 10:57:14 +00:00
# Exercise: Play with parameter passing
2021-10-17 10:51:50 +00:00
In this exercise we will look together at what happens when we change a reference
parameter to a value or a const reference.
2021-10-17 17:24:42 +00:00
Let's look at this function in [`Game.cpp`](../../../lib/Game.cpp)
2021-10-17 10:51:50 +00:00
```cpp
void Game::processEvents(InputState & inputState) {
auto event = canvas.pollEvent();
if (event && event.value().type == sf::Event::Closed) {
inputState.close = true;
return;
}
inputState.down = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down);
inputState.up = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up);
inputState.left = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left);
inputState.right = sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right);
}
```
## Exercise
2021-10-17 17:24:42 +00:00
You can follow along locally:
2021-10-17 10:51:50 +00:00
1. Make `inputState` a value. What happens when you compile the code? Can you explain why?
2. Now make it a `const` reference. What happens? Can you explain why?
3. Revert it back to a non-const reference an make sure the code compile.