codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
class Base { public: void print(){ cout << "base\n"; } }; class Derived { public: void print(){ cout << "derived\n"; } }; template<typename B, typename D> union poly { B* b; D* d; poly<B,D>(D* dp):d(dp){} poly<B,D>(B* bp):b(bp){} poly<B,D>& operator=(D* rhs){ this->d = rhs; return *this; } poly<B,D>& operator=(B* rhs){ this->b = rhs; return *this; } }; int main() { poly<Base,Derived> p = new Derived(); p.d->print(); p.b->print(); }
Private
[
?
]
Run code
Submit