Tune through an alias
Before you start this master track
Typed functions and collections. Be ready to reason about references, aliasing, and iterators.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
A reference gives an existing object another name. The ampersand in this declaration makes tuning refer to hertz instead of copying its initial value. Assigning through tuning therefore changes hertz. Both names observe the same integer, which is useful when a function or another scope must update an original value.
#include <iostream>int main() { int hertz = 432; int& tuning = hertz; tuning = 440; std::cout << hertz << " / " << tuning << '\n';}Each example is a complete C++17 console program with its own main function and required standard-library headers. Compile and run one mission at a time.
Track the values line by line. Predict the result before running this example in your own editor.