Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //NOT BY ME
- // http://stackoverflow.com/questions/13461869/c-push-multiple-types-onto-vector
- #include <iostream>
- #include <string>
- #include <vector>
- #include <memory>
- class any_type
- {
- public:
- virtual ~any_type() {}
- virtual void print() = 0;
- };
- template <class T>
- class concrete_type : public any_type
- {
- public:
- concrete_type(const T& value) : value_(value)
- {}
- virtual void print()
- {
- std::cout << value_ << '\n';
- }
- private:
- T value_;
- };
- int main()
- {
- std::vector<std::unique_ptr<any_type>> v(2);
- v[0].reset(new concrete_type<int>(99));
- v[1].reset(new concrete_type<std::string>("Bottles of Beer"));
- for(size_t x = 0; x < 2; ++x)
- {
- v[x]->print();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement