2 registered members (AndrewAMD, TedMar),
1,031
guests, and 1
spider. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Function overloading
#222706
08/20/08 15:51
08/20/08 15:51
|
Joined: Jan 2007
Posts: 221
Fenriswolf
OP
Member
|
OP
Member
Joined: Jan 2007
Posts: 221
|
Hello, in Lite-C (A7.10.1) function overloading does not work properly when the respective function takes a pointer as an argument (or so it seems; apparently there is a relation). Here is the thread that led to this conlusion. Example: The following code works without any problems: 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;
} However, when a pointer is added to the overloaded function as a second argument, overloading doesn't work anymore. In the following example the first function prototype is always used, leading to a crash. (typecast doesn't help) 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;
}
|
|
|
Re: Function overloading
[Re: Fenriswolf]
#222736
08/20/08 19:19
08/20/08 19:19
|
Joined: Jul 2004
Posts: 1,205 Greece
LarryLaffer
Serious User
|
Serious User
Joined: Jul 2004
Posts: 1,205
Greece
|
NULL is an int, not an int*, so you'll need to typecast it int test (char* s, int *i);
int test (int n, int *i);
void main () {
int num = 5;
char* str = "4";
printf("%i", test(num, (int*)NULL)); //return 5 (this will crash)
printf("%i", test(str, (int*)NULL)); //return 4
}
int test(char* s, int *i) {
return ((int)str_to_num(s));
}
int test(int n, int *i) {
return n;
} It's said that the overloading system may get a bit smarter in the feature, but for now, always typecast to avoid problems when you're overloading
|
|
|
Re: Function overloading
[Re: Fenriswolf]
#223262
08/23/08 01:03
08/23/08 01:03
|
Joined: Jul 2004
Posts: 1,205 Greece
LarryLaffer
Serious User
|
Serious User
Joined: Jul 2004
Posts: 1,205
Greece
|
Joozey, that's right. Fenriswolf, np I actually run into the same problem a couple of months ago. I was as puzzled as you were in this.. Cheers, Aris
|
|
|
|