Skip to content

Instantly share code, notes, and snippets.

@cloudwu
Last active August 19, 2023 02:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudwu/90bba8c5b154722fcb33f772bf9adeba to your computer and use it in GitHub Desktop.
Save cloudwu/90bba8c5b154722fcb33f772bf9adeba to your computer and use it in GitHub Desktop.
// interface
struct object {
object * create();
void release();
int getState();
};
// implementation
struct object_data : object {
int state = 0;
};
static inline object_data * cast_this(object *p) { return (object_data *)p; }
int
object::getState() {
auto self = cast_this(this);
return self->state;
}
object *
object::create() {
return new object_data;
}
void
object::release() {
auto self = cast_this(this);
delete(self);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment