#include class node { public: enum Tag { I, P }; private: union { int i; node * p; }; Tag tag; void check(Tag t){ if (tag!=t) {cout << "Bad access of node\n"; exit(1);}} public: Tag get_tag() { return tag; } int & ival() { check(I); return i; } node * & pval() { check(P); return p; } public: // Creating a new node node(int ii) { i=ii; tag=I; } node(node * pp) { p=pp; tag=P; } // Changing the value in a node void set(int ii) { i=ii; tag=I; } void set(node * pp) { p=pp; tag=P; } }; int main(){ node n(33); node * np; np = new node(&n); cout << "Should print 33: " << (np->pval())->ival() << endl; if (np->get_tag() == node::P) { cout << "Yes, *np is a pointer\n"; } np->set(45); if (np->get_tag() == node::I) { cout << "Now *np is the integer " << np->ival() << endl; } cout << "Next should be an error!\n"; (void) (np->pval()); return 0; }