[ create a new paste ] login | about

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

AaronMiller - C++, pasted on Sep 10:
#include <cstdio>
#include <cstddef>
#include <cstdlib>
#include <cstring>

typedef size_t uint;

class C {
private:
  uint m_refCnt;

public:
  C(): m_refCnt(1) {printf("C++\n");}
  virtual ~C() {printf("C--\n");}

  virtual void Grab() { m_refCnt++; }
  virtual void Drop() { if (--m_refCnt==0) delete this; }
};
class D: public virtual C {
protected:
  uint m_value;

public:
  D(): C(), m_value(0) {printf("D++\n");}
  virtual ~D() {printf("D--\n");}

  virtual void SetValue(uint x) { m_value=x; }
  virtual uint GetValue() const { return m_value; }

  inline void *operator new(uint n) { printf("new\n"); return malloc(n); }
  inline void operator delete(void *p) { printf("delete\n"); free(p); }
};

int main() {
  D *p;

  p = new D();
  p->SetValue(42);
  printf("%u\n", static_cast<unsigned int>(p->GetValue()));
  p->Drop();

  return EXIT_SUCCESS;
}


Output:
1
2
3
4
5
6
7
new
C++
D++
42
D--
C--
delete


Create a new paste based on this one


Comments: