Here is the example code:
- http://appcore.home.comcast.net/vzoo.../interface.zip
which is an analog of the following technique:
- http://groups.google.com/group/comp....f857051ca4029b
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_IObjec t_Destroy(void* const);
static int Triangle_IObjec t_Status(void* const);
static int Triangle_IShape _Draw(void* const);
/* Triangle IShape Interface VTable */
static IShape_VTable_t const Triangle_IShape _VTable = {
Triangle_IObjec t_Destroy,
Triangle_IObjec t_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(IT his, This, &Triangle_IShap e_VTable);
return 0;
}
return -1;
}
/* Triangle IObject Interface Functions */
int
Triangle_IObjec t_Destroy(void* const ThisState) {
Triangle_t* const This = ThisState;
free(This);
return 0;
}
int
Triangle_IObjec t_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(&My Shape);
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.