Function overloading

Posted By: Fenriswolf

Function overloading - 08/19/08 21:12

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?
Posted By: Joozey

Re: Function overloading - 08/19/08 21:44

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.
Posted By: Fenriswolf

Re: Function overloading - 08/19/08 21:57

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

Posted By: Joozey

Re: Function overloading - 08/19/08 22:38

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.
Posted By: Fenriswolf

Re: Function overloading - 08/19/08 23:12

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?
Posted By: Joozey

Re: Function overloading - 08/19/08 23:20

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.
Posted By: Fenriswolf

Re: Function overloading - 08/20/08 15:55

Done. : )
Thank you for your example. I have used it and the modified non-working version to describe the bug.
© 2024 lite-C Forums