[ create a new paste ] login | about

Link: http://codepad.org/GxK4RKNJ    [ raw code | output | fork ]

ninwa - C++, pasted on Oct 10:
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();
}


Output:
1
2
derived
base


Create a new paste based on this one


Comments: