Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, TedMar), 1,031 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Function overloading #222706
08/20/08 15:51
08/20/08 15:51
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

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:
Code:
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)
Code:
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 Offline
Serious User
LarryLaffer  Offline
Serious User

Joined: Jul 2004
Posts: 1,205
Greece
NULL is an int, not an int*, so you'll need to typecast it

Code:
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


INTENSE AI: Use the Best AI around for your games!
Join our Forums now! | Get Intense Pathfinding 3 Free!
Re: Function overloading [Re: LarryLaffer] #222742
08/20/08 19:40
08/20/08 19:40
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
So if the engine does not find a matching overloaded function, it takes the very first one?


Click and join the 3dgs irc community!
Room: #3dgs
Re: Function overloading [Re: LarryLaffer] #222743
08/20/08 19:45
08/20/08 19:45
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Yes, you are right; this solves the problem.
In my original example (from the other thread mentioned in the first post) I have to typecast the second parameter to void*.
Thank you for pointing this out, Larry!

Re: Function overloading [Re: Fenriswolf] #223262
08/23/08 01:03
08/23/08 01:03
Joined: Jul 2004
Posts: 1,205
Greece
LarryLaffer Offline
Serious User
LarryLaffer  Offline
Serious User

Joined: Jul 2004
Posts: 1,205
Greece
Joozey, that's right.

Fenriswolf, np smile I actually run into the same problem a couple of months ago. I was as puzzled as you were in this..

Cheers,
Aris


INTENSE AI: Use the Best AI around for your games!
Join our Forums now! | Get Intense Pathfinding 3 Free!

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1