struct plane
{
Vector3D loc;
float speed;
}
plane biPlane = new plane();
biPlane->IncreaseSpeed(2);
struct plane
{
Vector3D loc;
float speed;
}
plane biPlane = new plane();
biPlane->IncreaseSpeed(2);
Quote:
I wish to understand, do I need a implementation file and a header file for plane
#include <stdio.h>struct plane{ float m_speed; void IncreaseSpeed (void) { m_speed++; } void SetSpeed (float speed) { m_speed = speed; } void Initialise(void) { m_speed = 0; }};int main (void){ plane biPlane; biPlane.Initialise(); biPlane.IncreaseSpeed(); printf("m_speed: %f\n", biPlane.m_speed); biPlane.SetSpeed(5.0); printf("m_speed: %f\n", biPlane.m_speed); return 0;}
biPlane->Initialise();biPlane->IncreaseSpeed();biPlane->SetSpeed(5.0);
struct type{ int value;};void t_constructor(struct type *t,int value){ t->value=value;}void t_method(struct type *t,int add){ t->value+=add;}void f(){ type t; t_constructor(&t,10); t_method(&t,23);}
struct type{ int value; void (*method)(struct type*,int);};void t_method_one(struct type *t,int add){ t->value+=add;}void t_method_two(struct type *t,int sub){ t->value-=sub;}void t_add_constructor(struct type *t,int value){ t->value=value; t->method=t_method_one;}void t_sub_constructor(struct type *t,int value){ t->value=value; t->method=t_method_two;}void poly(struct type *t){ (*t->method)(t,23); // think this is correct syntax, can't remember}void f(){ struct type a,b; t_add_constructor(&a,10); t_sub_constructor(&b,20); poly(&a); poly(&b);}
Quote:Original post by BlackDuck
I have a project that can only be done in C. ...
Quote:Original post by BlackDuck
Please review code. Please correct me, but I believe I have create a struct that contains a member and three methods..kind of...class speaking ;) maybe? ...
struct plane{ Vector3D loc; float speed;}void planeNew(plane* p){ // do your initialization here p->speed = 0; p->loc.x = 0; p->loc.y = 0; p->loc.z = 0;}void planeIncreaseSpeed(plane* p, float amount){ p->speed += amount;}...plane players[NUM_PLAYERS];...void setup(){ for (i = 0; i < NUM_PLAYERS; i++) { planeNew(&players); }}...void onSpeedUp(int playerId){ planeIncreaseSpeed(&players[playerId], 2);}
Quote:Original post by szecs
It is in fact useful to have function pointer members in a struct: think about a GUI structure. Drawing/function/whatever are just pointers, so the GUI will be flexible, and not the immediate mode stuff
Quote:Original post by BlackDuckI think it isn't really a question whether to use a dot or an arrow. Even in C++ you have both ways, dependend on whether you have a reference or else a pointer to the class instance. Instead, the exercise is just to declare a struct where function pointers are members, to set such pointers to an actual function, and to invoke it.
Now I only seem to be able to call the function like foo.bar rather than foo->bar
Is there a way?