Redefining the same function in three way.

Posted By: Emre

Redefining the same function in three way. - 04/06/20 05:35

Hi everybody. I have to ask this question because my programming knowledge is not good.

i want to use the skill of panel as strings, as follows;

Code
test_panel.skill_x=str_create("");


No problem but every time i want to perform string operation, i have to specify that the skill is STRING*.
Code
str_cpy((STRING*)test_panel.skill_x,"test");

But i don't want to do that because it's annoying. So i redefined (i don't know if this is right word) str_cpy function in three different ways like this;

Code
#ifndef str_cpy
	function str_cpy(var var1,STRING* str)
	{
		str_cpy((STRING*)var1,str);
	}
	function str_cpy(STRING* str,var var1)
	{
		str_cpy(str,(STRING*)var1);
	}
	function str_cpy(var var1,var var2)
	{
		str_cpy((STRING*)var1,(STRING*)var2);
	}
#endif


it works without problem. With this way, i can use "str_cpy" function for copy "string to skill" or opposite, or "skill to skill" without use STRING* pointer.


My question: is there any harm in this method? is it safe using this way?
Posted By: Emre

Re: Redefining the same function in three way. - 04/07/20 13:07

Ok. i found the answer. Function overloading.
Posted By: txesmi

Re: Redefining the same function in three way. - 04/09/20 14:42

Hi,
I would go with macros. Something like this:

Code
#define s_cpy(x, y)  str_cpy((STRING*)x, (STRING*)y)


And then use 's_cpy' instead of 'str_cpy' for every kind of data. It allows using char pointers or literals as second parameter too.

Salud!
Posted By: Emre

Re: Redefining the same function in three way. - 04/09/20 17:42

Using macros seems more sensible indeed. Thank you, txesmi!
Posted By: txesmi

Re: Redefining the same function in three way. - 04/09/20 18:35

Glad of been of help laugh
© 2024 lite-C Forums