/* Polymorphic stack example in C++ from pages 246-247 of Kenneth C. Louden, Programming Languages Principles and Practice 2nd Edition Copyright (C) Brooks-Cole/ITP, 2003 */ #include using namespace std; template struct StackNode { T data; StackNode * next; }; template struct Stack { StackNode * theStack; }; template T top (Stack s) { return s.theStack->data; } int main() { Stack s; s.theStack = new StackNode; s.theStack->data = 3; s.theStack->next = 0; int t = top(s); // t is now 3 cout << t << endl; return 0; }