Skip to content

Instantly share code, notes, and snippets.

@grundprinzip
Created July 2, 2011 06:32
Show Gist options
  • Save grundprinzip/1059800 to your computer and use it in GitHub Desktop.
Save grundprinzip/1059800 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class A
{
protected:
public:
int count;
A() : count(1) {
};
void retain() { ++count; }
void release()
{
cout << "RELEASE" << endl;
count--;
if (count == 0) {
cout << "COUNT == 0" << endl;
delete this;
}
}
virtual ~A() { cout << "~A" << endl; }
void operator delete(void* ptr, size_t s)
{
cout << "A" << endl;
}
};
class B : public A
{
public:
virtual ~B() { cout << "~B" << endl; }
void operator delete(void* ptr, size_t s)
{
cout << "B" << endl;
}
};
template<typename T>
class C : public B
{
public:
virtual ~C() { cout << "~C" << endl; }
void operator delete(void* ptr, size_t s)
{
A* a = (A *) ptr;
cout << a->count << endl;
if (a->count > 1 ) {
cout << "releasing" << endl;
a->release();
} else {
cout << "C" << sizeof(T) << endl;
}
}
};
int main()
{
B* b = new C<int>;
b->retain();
b->release();
//b->release();
delete b;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment