Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- template<typename T>
- class final
- {
- private:
- T data;
- bool used;
- public:
- final(): used(false)
- {
- }
- final(const T &a)
- {
- used = true;
- data = a;
- }
- final(const final<T> &a)
- {
- used = true;
- data = a.get();
- }
- const T & operator=(const T &a)
- {
- if (used)
- throw std::runtime_error("Hey! I'm final!");
- else
- {
- used = true;
- data = a;
- }
- return data;
- }
- const final<T> & operator=(const final<T> &a)
- {
- if (used)
- throw std::runtime_error("Hey! I'm final!");
- else
- {
- used = true;
- data = a.get();
- }
- return *this;
- }
- const T & operator()() const
- {
- return data;
- }
- };
- int main()
- {
- final<int> a;
- a = 5;
- std::cout << a();
- a = 7;
- std::cout << a();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement