Skip to content

Instantly share code, notes, and snippets.

@TvdW
Created July 9, 2011 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TvdW/1073158 to your computer and use it in GitHub Desktop.
Save TvdW/1073158 to your computer and use it in GitHub Desktop.
Sample C++ memory object
class Object {
public:
int retainCount;
void alloc();
virtual void dealloc();
void release();
void retain();
};
// Actually, alloc() isn't the right word. The actual allocation is done by using "new Object" in your code.
void Object::alloc() {
retainCount = 0;
}
virtual void Object::dealloc() {
delete this; // Remove ourselves from the memory
}
void Object::release() {
retainCount--;
if (retainCount < 0) {
dealloc();
}
}
void Object::retain() {
retainCount++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment