Skip to content

Instantly share code, notes, and snippets.

@bastih
Forked from grundprinzip/gist:1057995
Created July 1, 2011 06:47
Show Gist options
  • Save bastih/1057998 to your computer and use it in GitHub Desktop.
Save bastih/1057998 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class A
{
protected:
public:
int count;
A() : count(1) {
};
void release()
{
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;
if (a->count == 0) {
cout << "C" << sizeof(T) << endl;
} else {
cout << "releasing" << endl;
a->release();
}
}
};
int main()
{
C<int>* b = new C<int>;
b->release();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment