Advertisement
mskf

Untitled

Jun 4th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>
  7. class A{
  8. private:
  9. A();
  10.  
  11. public:
  12. A(const T &object);
  13. const T&fetch(const T&or_else = Default) const noexcept;
  14.  
  15. public:
  16. static const T Default;
  17.  
  18. private:
  19. T contents;
  20. bool has_something;
  21. };
  22.  
  23. template<typename T>
  24. const T A<T>::Default{"DEFAULT"};
  25.  
  26. template<typename T>
  27. A<T>::A():contents{Default}, has_something{false}{}
  28.  
  29. template<typename T>
  30. A<T>::A(const T &object):contents{object}, has_something{true}{}
  31.  
  32. template<typename T>
  33. const T& A<T>::fetch(const T &or_else) const noexcept{
  34. if ( has_something )
  35. return contents;
  36. else
  37. return or_else;
  38. }
  39.  
  40. int main()
  41. {
  42. cout << "\nTest 1" << endl;
  43. A< string > hibox{ "Hello, world!" };
  44. cout << hibox.fetch() << endl;
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement