2 registered members (Akow, SBGuy),
1,423
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: frankjiang]
#340343
09/03/10 11:05
09/03/10 11:05
|
Joined: Feb 2009
Posts: 2,154
Damocles_
Expert
|
Expert
Joined: Feb 2009
Posts: 2,154
|
This works: (using int) (the includes are not perfect..)
#include <default.c>
#include <litec.h>
typedef struct
{
int x;
int y;
void fn(int x); //The function pointer
} SPOT;
void func(int x) //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
SPOT* spot = setup_spot(); //create SPOT
spot->fn(5); //call func();
}
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: Damocles_]
#340346
09/03/10 11:33
09/03/10 11:33
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
@damocles: It does? Is that too for A7? I was under the impression that it was not possible to call structure elements as functions... I always do this, seems to work just fine, but not sure if it could go wrong with an integer pointer as function pointer storage:
typedef struct
{
int *spot_func;
} SPOT;
void spot_func_prototype(SPOT *s);
...
void spot_remove(SPOT s)
{
error("yay");
}
void main()
{
SPOT spot;
spot.func = spot_remove;
spot_func_prototype = spot.func;
spot_func_prototype( spot );
}
Last edited by Joozey; 09/03/10 11:35.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: Damocles_]
#340353
09/03/10 12:16
09/03/10 12:16
|
Joined: Feb 2009
Posts: 2,154
Damocles_
Expert
|
Expert
Joined: Feb 2009
Posts: 2,154
|
Works also with return values. Basically you can almost work as in an object oriented approach.
#include <default.c>
typedef struct spotpoint
{
int x;
int y;
int fn(struct spotpoint * myself, int add_x); //The function pointer
} SPOT;
int func(SPOT* myself, int add_x) //your function
{
int itis=myself->x + add_x;
return itis;
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
//create the objects
SPOT* spot = setup_spot(); //create SPOT
SPOT* spot2 = setup_spot(); //create SPOT
//alter attributes in objects
spot->x=4; //set values
spot2->x=3; //set values
//call function in objects, so they work on their personal attributes
int whatis= spot->fn(spot,5); //call func();
int whatis2= spot2->fn(spot2,5); //call func();
if(whatis==9) error("Correct value for spot returned!");
else error("False value spot returned!");
if(whatis2==9) error("Correct value spot2 returned!");
else error("False value spot2 returned!");
}
Last edited by Damocles_; 09/03/10 12:34.
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: Joozey]
#342068
09/23/10 06:58
09/23/10 06:58
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Write a runtime that gets called wenn calling a function in a struct. In this function set a pointer called this to the struct and then call the actual function.
It's that easy.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
|