Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <iostream>
- template <class T>
- class Stack {
- struct Link {
- Link(T *d, Link *nxt) : dat(d), next(nxt) {}
- T *dat;
- Link *next;
- } *head;
- public:
- void push(T* el) {
- Link* l = new Link(el, head);
- head = l;
- }
- T* peek() {
- return head->dat;
- }
- T* pop() {
- Link* oldDat = head;
- T* data = head->dat;
- head = next->head;
- delete oldDat;
- return head->dat;
- }
- friend class Iterator;
- };
- class Iterator {
- Stack::Link *sp;
- public:
- Iterator(const Stack<T> &s) : p(s.head) {}
- Iterator() : p(0) {}
- };
- class Foo {
- int z;
- public:
- Foo(int zz) : z(zz) {
- std::cout<<"Foo ctor! "<<z<<std::endl;
- }
- friend std::ostream& operator<< (std::ostream &s, const Foo & f) {
- return s<<f.z;
- }
- };
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- Foo *f1 = new Foo(228);
- Stack<Foo> fooStack;
- fooStack.push(f1);
- Foo *z = fooStack.peek();
- std::cout<<*z;
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement