[ create a new paste ] login | about

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

GiM - C++, pasted on Nov 15:
#include <iostream>

using std::cout;
using std::endl;

//non-virtual dtors
struct Bar {
protected:
  void *x;
  ~Bar() { cout << "in Bar dtor: " << x << endl; }
public:
  Bar(void *x) : x(x) { cout << "in Bar ctor: " << x << endl; }
};

struct Baz : public Bar {
  Baz(void *x) : Bar(x) { cout << "in Baz ctor: " << x << endl; }
protected:
  ~Baz() { cout << "in Baz dtor" << x << endl; }
};

struct Dummy {
  int d;
  void dummy() { cout << "dummy: d" << endl; }
};

template <class Base>
struct Foo : public Dummy, public Base
{
  Foo(void *z) : Base(z) {}
};

typedef Foo<Bar> Foor;
typedef Foo<Baz> Fooz;

int main(int a, char** b)
{
  Foor* fr = new Foor((void*)123);
  Fooz* fz = new Fooz((void*)456);

  fr->dummy();
  fz->dummy();

  delete fr;
  
  fr = (Foor*)fz;
  delete fr;

  cout << "after delete" << endl;

  return 0;
}


Output:
1
2
3
4
5
6
7
8
in Bar ctor: 0x7b
in Bar ctor: 0x1c8
in Baz ctor: 0x1c8
dummy: d
dummy: d
in Bar dtor: 0x7b
in Bar dtor: 0x1c8
after delete


Create a new paste based on this one


Comments: