Created
July 5, 2012 11:25
-
-
Save r2p2/3053128 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <typeinfo> | |
| #include <vector> | |
| #include <tr1/memory> | |
| struct Base | |
| { | |
| virtual void set(const Base &b) | |
| { throw "not implemented"; } | |
| virtual Base& index() const | |
| { throw "not implemented"; } | |
| virtual Base* clone() const | |
| { throw "not implemented"; } | |
| }; | |
| struct Derived : Base | |
| { | |
| virtual Base* clone() const | |
| { return new Derived(*this); } | |
| }; | |
| struct List : Base | |
| { | |
| List() | |
| {} | |
| List(const List &orig) | |
| { _x.push_back(std::tr1::shared_ptr<Base>(orig._x[0]->clone()));} | |
| virtual void set(const Base &b) | |
| { _x.push_back(std::tr1::shared_ptr<Base>(b.clone())); } | |
| virtual Base& index() const | |
| { | |
| std::vector<std::tr1::shared_ptr<Base> >::const_iterator it = _x.begin(); | |
| return *((*it).get()); | |
| } | |
| virtual Base* clone() const | |
| { return new List(*this); } | |
| std::vector<std::tr1::shared_ptr<Base> > _x; | |
| }; | |
| int main() | |
| { | |
| try | |
| { | |
| Derived d; | |
| List sublist; | |
| sublist.set(d); | |
| List l; | |
| l.set(sublist); | |
| Base &b = l.index(); | |
| std::cout << "b knows that it is a list: " << typeid(b).name() << std::endl; | |
| std::cout << "but index is not implemented???" << std::endl; | |
| l.index().index(); | |
| } | |
| catch(const char *e) | |
| { | |
| std::cerr << "ERROR: " << e << std::endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment