|
|
well..you can simulate all the effects of c++ based object orientation in C using function pointers..but yah..everything's done manually ..
you construct manually.. you destruct manually... etc..
when you construct manually .. you associate the fp's there..
like .. (and i hope i remember my function pointer syntax right, here, i just don't use it very often in c++ :) .. the language takes care of all that for me :P )
:
typedef struct vector
{
/*no data hiding, since c has no concept of privacy ;) */
float x,y,z;
vector* (*add)(vector *p_this , vector *p_rhs);
float (*dot)(vector *p_this, vector *p_rhs);
vector* (*cross) (vector *p_this , vector *p_rhs);
};
vector* vector_add( vector *p_this , vector *p_rhs)
{
/*do add*/
}
float vector_dot_product (vector *p_this, vector *p_rhs){}
vector* vector_cross_product (vector *p_this , vector *p_rhs){}
vector_create( vector *p,float x, float y, float z)
{
p->add = vector_add;
p->dot = vector_dot_product;
p->cross = vector_cross_product;
/*and other ops, like subtract & whatnot */
p->x = x;
p->y = y;
p->z = z;
}
int main()
{
vector p, q;
vector_create(&p, 1.0, 2.0 , 3.0);
vector_create(&q , 3.0 , 15.2 , 0.0);
float d = p.dot(p,q);
printf ("wouldn't overloaded >> and << be nicer than this? ;), %s,%s,%s",p.x,p.y,p.z);
}
--vat
|