2 registered members (TedMar, AndrewAMD),
1,067
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Function overloading
#222539
08/19/08 21:12
08/19/08 21:12
|
Joined: Jan 2007
Posts: 221
Fenriswolf
OP
Member
|
OP
Member
Joined: Jan 2007
Posts: 221
|
Hello, the problem is simple: I just can't get overloaded functions working.
// Prototypes for overloaded function
AVL_ELEM *avlElem_create(int i_key, void *p_data);
AVL_ELEM *avlElem_create(char *c_key, void *p_data);
/*
* ...
*/
avlElem_create((int)12, data); // first function gets called
avlElem_create((char*)"hello", data); // first function gets called again
The first function (which accepts an integer) is always used, even if typecasting a char as first parameter. If I change the order of the prototypes the other function is always used instead. Does somebody know what I'm doing wrong here?
|
|
|
Re: Function overloading
[Re: Fenriswolf]
#222545
08/19/08 21:44
08/19/08 21:44
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Hm interesting behaviour. See if it does work using a STRING instead of a char. If so, I'd say it's because int and char datatypes are, someway or another, treated or recognised the same way in lite-c.
Last edited by Joozey; 08/19/08 21:44.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Function overloading
[Re: Joozey]
#222552
08/19/08 21:57
08/19/08 21:57
|
Joined: Jan 2007
Posts: 221
Fenriswolf
OP
Member
|
OP
Member
Joined: Jan 2007
Posts: 221
|
Obviously this doesn't change anything. This is the modified code:
// Prototypes for overloaded function
AVL_ELEM *avlElem_create(int i_key, void *p_data);
AVL_ELEM *avlElem_create(STRING *str_key, void *p_data);
/*
* ...
*/
avlElem_create((int)12, data); // first function gets called
avlElem_create((STRING*)str_create("hello"), data); // first function gets called again
|
|
|
Re: Function overloading
[Re: Fenriswolf]
#222562
08/19/08 22:38
08/19/08 22:38
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Well, obviously? I do have overloading to work perfectly fine. int test (char* s);
int test (int n);
void main () {
int num = 5;
char* str = "4";
printf("%i", test(num)); //return 5
printf("%i", test(str)); //return 4
}
int test(char* s) {
return ((int)str_to_num(s));
}
int test(int n) {
return n;
} Should be the same setting as you, right? first printed value is 5, second is 4.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Function overloading
[Re: Joozey]
#222567
08/19/08 23:12
08/19/08 23:12
|
Joined: Jan 2007
Posts: 221
Fenriswolf
OP
Member
|
OP
Member
Joined: Jan 2007
Posts: 221
|
Yes, your example works fine. However, when adding a pointer (type doesn't seem to matter) as a second argument it doesn't work anymore (shows same behavior as in my case): int test (char* s, int *i);
int test (int n, int *i);
void main () {
int num = 5;
char* str = "4";
printf("%i", test(num, NULL)); //return 5 (this will crash)
printf("%i", test(str, NULL)); //return 4
}
int test(char* s, int *i) {
return ((int)str_to_num(s));
}
int test(int n, int *i) {
return n;
} This code will crash now because the first prototype function is called in both cases. Perhaps this is a bug?
|
|
|
|