4 registered members (dBc, clonman, TipmyPip, 1 invisible),
18,936
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Function pointer in struct with more argument types
#431962
10/27/13 09:40
10/27/13 09:40
|
Joined: Apr 2006
Posts: 159 Latvija
Arrovs
OP
Member
|
OP
Member
Joined: Apr 2006
Posts: 159
Latvija
|
I ran into problem when i just cant think out that what im showing in example. Example:
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.
Arrovs once will publish game
|
|
|
Re: Function pointer in struct with more argument types
[Re: Arrovs]
#431963
10/27/13 10:04
10/27/13 10:04
|
Malice
Unregistered
|
Malice
Unregistered
|
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
Last edited by Malice; 10/27/13 10:06.
|
|
|
Re: Function pointer in struct with more argument types
[Re: ]
#431965
10/27/13 12:57
10/27/13 12:57
|
Joined: Nov 2007
Posts: 318 Brasil, ParanĂ¡
NeoNeper
Senior Member
|
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, ParanĂ¡
|
#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);
}
Last edited by NeoNeper; 10/27/13 13:02.
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
Re: Function pointer in struct with more argument types
[Re: NeoNeper]
#431973
10/27/13 17:19
10/27/13 17:19
|
Joined: Apr 2006
Posts: 159 Latvija
Arrovs
OP
Member
|
OP
Member
Joined: Apr 2006
Posts: 159
Latvija
|
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.
Arrovs once will publish game
|
|
|
Re: Function pointer in struct with more argument types
[Re: Arrovs]
#431974
10/27/13 17:40
10/27/13 17:40
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
You cannot at the moment and you shouldn't. Use unique function names instead.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Function pointer in struct with more argument types
[Re: Uhrwerk]
#434087
12/12/13 03:27
12/12/13 03:27
|
Joined: Jun 2004
Posts: 2,234 Wisconsin USA
FoxHound
Expert
|
Expert
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
|
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.
--------------------- There is no signature here.
QUIT LOOKING FOR ONE!
|
|
|
Re: Function pointer in struct with more argument types
[Re: FoxHound]
#434089
12/12/13 08:06
12/12/13 08:06
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
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. 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.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Function pointer in struct with more argument types
[Re: Uhrwerk]
#434128
12/13/13 22:35
12/13/13 22:35
|
Joined: Jun 2004
Posts: 2,234 Wisconsin USA
FoxHound
Expert
|
Expert
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
|
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.
--------------------- There is no signature here.
QUIT LOOKING FOR ONE!
|
|
|
|