[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppSwitch
If you have a class that uses a state-dependent switch-statement, use polymorphism instead [1].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppSwitch
(C++) switch
Keywords enabling control over program flow, used in combanation with the case and default keywords.int main() { const int diceThrow = std::rand()%6; switch(diceThrow) { case 0: std::cout << "You threw 1 and lost!" << std::endl; break; case 5: std::cout << "You threw 6 and won!" << std::endl; break; default: std::cout << "You threw something useless!" << std::endl; break; } }
- include <iostream>
If you have a class that uses a state-dependent switch-statement, use polymorphism instead [1].
Code links
'switch' links
Reference
- 1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 90: 'Avoid type switching; prefer polymorphism'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
