(C++) switch
February 24, 2017 · View on GitHub
(C++) switch
switch is a keyword to perform a kind of multiple if statements on a certain value. case determines which values can be switch on. If there is no named value to switch on, default can be used optionally.
Example
Advice
- Prefer a switch-statement to an if-statement when there is a choice [1]
- Prefer virtual functions to repeated switch-statements based on typeid or dynamic_cast [2]
References
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 9.8. Advice, page 240: '[2] Prefer a switch-statement to an if-statement when there is a choice'
- Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 22.7. Advice. page 663: '[10] Prefer virtual functions to repeated switch-statements based on typeid or dynamic_cast'