Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- template<typename T>
- class A{
- private:
- A();
- public:
- A(const T &object);
- const T&fetch(const T&or_else = Default) const noexcept;
- public:
- static const T Default;
- private:
- T contents;
- bool has_something;
- };
- template<typename T>
- const T A<T>::Default{"DEFAULT"};
- template<typename T>
- A<T>::A():contents{Default}, has_something{false}{}
- template<typename T>
- A<T>::A(const T &object):contents{object}, has_something{true}{}
- template<typename T>
- const T& A<T>::fetch(const T &or_else) const noexcept{
- if ( has_something )
- return contents;
- else
- return or_else;
- }
- int main()
- {
- cout << "\nTest 1" << endl;
- A< string > hibox{ "Hello, world!" };
- cout << hibox.fetch() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement