Function pointer in struct with more argument types

Posted By: Arrovs

Function pointer in struct with more argument types - 10/27/13 09:40

I ran into problem when i just cant think out that what im showing in example.
Example:
Code:
typedef struct
{
function test();
function test(int i);
}Function_struct

function main()
{
//all initialization
//now adding function to pointer
function_struct.test = some_function;
//so how i can get it to recognize both argument types?
//Like button click function can work with and without arguments
}



Possible i need to use other aproach, but its just example.

I really hope any can give good working one.
Posted By: Anonymous

Re: Function pointer in struct with more argument types - 10/27/13 10:04

I'm going to ask because I'd like to know for sure.
I thought a Struct couldn't have functions in it - That would be a class?

I asking not telling -
Thanks
Mal
Posted By: NeoNeper

Re: Function pointer in struct with more argument types - 10/27/13 12:57

Code:
#define new(type) malloc(sizeof(type##))



typedef struct
{

void fCallback(char *msg);

}DATA;

void vtest2(char* a)
{
	
printf(a);
	
}


function main()
{
	

	level_load(NULL);
	wait(5);
	video_window(nullvector, nullvector, 16|32|64|128, "My Test");
   
   
	DATA* mydata = new(DATA);
	mydata.fCallback=vtest2;
	mydata.fCallback("Hello World");
	
	wait(1);
	free(mydata);
}



Posted By: Arrovs

Re: Function pointer in struct with more argument types - 10/27/13 17:19

NeoNeper: Im using this aprouch already for some time, but im asking how to get it to recognize different paramters.

function test();
function test(int i);

like this two. In struct i can show how many paramters i use for function and it can recognize it, but when i want to let it recognize all types under same name i dont know how to do it.

Malice: Just see NeoNeper example.
Posted By: Uhrwerk

Re: Function pointer in struct with more argument types - 10/27/13 17:40

You cannot at the moment and you shouldn't. Use unique function names instead.
Posted By: FoxHound

Re: Function pointer in struct with more argument types - 12/12/13 03:27

I know this is old but the info here is wrong.

You can have function pointers in your structs.

typedef struct
{

void* fCallback;
}DATA;


DATA* mydata = new(DATA);
mydata.fCallback=vtest2;
mydata.fCallback("Hello World");

The manual goes into detail on how to use function pointers.
Posted By: Uhrwerk

Re: Function pointer in struct with more argument types - 12/12/13 08:06

Foxhound, please don't mix C++ and lite c. There is no new operator in lite c. Structs are getting allocated with sys_malloc or one of its relatives. Also you cannot call function pointers directly in lite c. You have to assign them to a prototype first.
Originally Posted By: Malice
I thought a Struct couldn't have functions in it - That would be a class?
As said, a struct can have abitrary pointers to whatever. But that doesn't make it a class. Structs neither support inheritance, nor encapsulation or polymorphism.
Posted By: FoxHound

Re: Function pointer in struct with more argument types - 12/13/13 22:35

I'm not. This is not a function in a class but a function pointer to be kept in a struct. I have several of them in my own code (lite c) and use them quite often.
© 2024 lite-C Forums