Created
July 9, 2011 00:55
-
-
Save TvdW/1073158 to your computer and use it in GitHub Desktop.
Sample C++ memory object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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