C
Chris Thomasson
Here is the example code:
- http://appcore.home.comcast.net/vzoom/example/interface.zip
which is an analog of the following technique:
- http://groups.google.com/group/comp.lang.c/msg/6cf857051ca4029b
It's definitely not full-blown OOP is any sense of the term, however imho, I
think it could be fairly useful in certain scenarios... Now, let me try to
briefly describe it:
The provided example shows how a minimalist abstract interface for a "shape"
object might look. It also includes an implementation of simple circle and
square objects that are compatible with the abstract shape interface. If you
want to try and add another shape to get a feel for how "cumbersome" the
interface method is, well, here is a quick example of that:
<pseudo-code for adding, lets say, a triangle>
__________________________
triangle.h
__________________
/* Put include guard here */
#include "ishape.h"
/* Return values that are non-zero indicate error's */
/* Create a new triangle */
extern int Triangle_Create(IShape_t* const, /* [triangle params] */);
triangle.c
__________________
#include "triangle.h"
typedef struct Triangle_s Triangle_t;
struct Triangle_s {
/* [triangle members] */
};
static int Triangle_IObject_Destroy(void* const);
static int Triangle_IObject_Status(void* const);
static int Triangle_IShape_Draw(void* const);
/* Triangle IShape Interface VTable */
static IShape_VTable_t const Triangle_IShape_VTable = {
Triangle_IObject_Destroy,
Triangle_IObject_Status,
Triangle_IShape_Draw
};
/* Triangle Interface Functions */
int
Triangle_Create(
IShape_t* const IThis,
/* [triangle params] */) {
Triangle_t* const This = malloc(sizeof(*This));
if (This) {
/* [init This triangle] */
IOBJECT_INIT(IThis, This, &Triangle_IShape_VTable);
return 0;
}
return -1;
}
/* Triangle IObject Interface Functions */
int
Triangle_IObject_Destroy(void* const ThisState) {
Triangle_t* const This = ThisState;
free(This);
return 0;
}
int
Triangle_IObject_Status(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [determine This triangle state; this is optional] */
return 0;
}
/* Triangle IShape Interface Functions */
int
Triangle_IShape_Draw(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [draw This triangle] */
return 0;
}
__________________________
IMHO, that should be "fairly" straight forward... Now, here is how you could
use your Triangle:
__________________________
#include "triangle.h"
int Example(void) {
IShape_t MyShape;
int rStatus = Triangle_Create(&MyShape, /* [triangle params] */);
if (! rStatus) {
rStatus = IShape_Draw(&MyShape);
if (rStatus) {
rStatus = IObject_Status(&MyShape);
}
rStatus = IObject_Destroy(&MyShape);
}
return rStatus;
}
__________________________
I was wondering if you have come across any superior methods for very basic
OOP in C?
Any thoughts? Is this method total crap?
;^)
Thanks.
- http://appcore.home.comcast.net/vzoom/example/interface.zip
which is an analog of the following technique:
- http://groups.google.com/group/comp.lang.c/msg/6cf857051ca4029b
It's definitely not full-blown OOP is any sense of the term, however imho, I
think it could be fairly useful in certain scenarios... Now, let me try to
briefly describe it:
The provided example shows how a minimalist abstract interface for a "shape"
object might look. It also includes an implementation of simple circle and
square objects that are compatible with the abstract shape interface. If you
want to try and add another shape to get a feel for how "cumbersome" the
interface method is, well, here is a quick example of that:
<pseudo-code for adding, lets say, a triangle>
__________________________
triangle.h
__________________
/* Put include guard here */
#include "ishape.h"
/* Return values that are non-zero indicate error's */
/* Create a new triangle */
extern int Triangle_Create(IShape_t* const, /* [triangle params] */);
triangle.c
__________________
#include "triangle.h"
typedef struct Triangle_s Triangle_t;
struct Triangle_s {
/* [triangle members] */
};
static int Triangle_IObject_Destroy(void* const);
static int Triangle_IObject_Status(void* const);
static int Triangle_IShape_Draw(void* const);
/* Triangle IShape Interface VTable */
static IShape_VTable_t const Triangle_IShape_VTable = {
Triangle_IObject_Destroy,
Triangle_IObject_Status,
Triangle_IShape_Draw
};
/* Triangle Interface Functions */
int
Triangle_Create(
IShape_t* const IThis,
/* [triangle params] */) {
Triangle_t* const This = malloc(sizeof(*This));
if (This) {
/* [init This triangle] */
IOBJECT_INIT(IThis, This, &Triangle_IShape_VTable);
return 0;
}
return -1;
}
/* Triangle IObject Interface Functions */
int
Triangle_IObject_Destroy(void* const ThisState) {
Triangle_t* const This = ThisState;
free(This);
return 0;
}
int
Triangle_IObject_Status(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [determine This triangle state; this is optional] */
return 0;
}
/* Triangle IShape Interface Functions */
int
Triangle_IShape_Draw(void* const ThisState) {
Triangle_t* const This = ThisState;
/* [draw This triangle] */
return 0;
}
__________________________
IMHO, that should be "fairly" straight forward... Now, here is how you could
use your Triangle:
__________________________
#include "triangle.h"
int Example(void) {
IShape_t MyShape;
int rStatus = Triangle_Create(&MyShape, /* [triangle params] */);
if (! rStatus) {
rStatus = IShape_Draw(&MyShape);
if (rStatus) {
rStatus = IObject_Status(&MyShape);
}
rStatus = IObject_Destroy(&MyShape);
}
return rStatus;
}
__________________________
I was wondering if you have come across any superior methods for very basic
OOP in C?
Any thoughts? Is this method total crap?
;^)
Thanks.