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
4 registered members (degenerate_762, Aku_Aku, 7th_zorro, Ayumi), 1,080 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 #222539
08/19/08 21:12
08/19/08 21:12
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Hello,

the problem is simple: I just can't get overloaded functions working.

Code:
// 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 Offline
Expert
Joozey  Offline
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
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Obviously this doesn't change anything.

This is the modified code:
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 Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Well, obviously? I do have overloading to work perfectly fine.

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;
}


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
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

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):
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;
}

This code will crash now because the first prototype function is called in both cases.

Perhaps this is a bug?

Re: Function overloading [Re: Fenriswolf] #222569
08/19/08 23:20
08/19/08 23:20
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
I can verify that it crashes. It certainly looks like a bug, and nothing about it in the bug report. Can't really think of anything else why this should happen, so I would open a bug thread about it wink.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Function overloading [Re: Joozey] #222707
08/20/08 15:55
08/20/08 15:55
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline OP
Member
Fenriswolf  Offline OP
Member
F

Joined: Jan 2007
Posts: 221
Done. : )
Thank you for your example. I have used it and the modified non-working version to describe the bug.


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