fork download
  1. #include <iostream>
  2. using namespace std;
  3. template <typename T>
  4. class Interleave {
  5. T a, b;
  6. bool check = false;
  7. public:
  8. Interleave(T a, T b) {
  9. this->a = a;
  10. this->b = b;
  11. }
  12.  
  13. T getValue() {
  14. check = !check;
  15. return check ? a : b;
  16. }
  17. };
  18.  
  19. int getNumber() {
  20. static Interleave<int> numero(10, 20);
  21. return numero.getValue();
  22. }
  23.  
  24. int main() {
  25. Interleave<int> numero(10, 20);
  26. cout << numero.getValue() << endl;
  27. cout << numero.getValue() << endl;
  28. cout << numero.getValue() << endl;
  29.  
  30. Interleave<string> texto("aaa", "bbb");
  31. cout << texto.getValue() << endl;
  32. cout << texto.getValue() << endl;
  33. cout << texto.getValue() << endl;
  34.  
  35. cout << getNumber() << endl;
  36. cout << getNumber() << endl;
  37. cout << getNumber() << endl;
  38. }
  39.  
  40. //https://pt.stackoverflow.com/q/45723/101
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
10
20
10
aaa
bbb
aaa
10
20
10