Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i f i pass the value as integer to the given function as
int add(int,int)
float add(int,int)

then what it will return as output as it give float/integer
Posted
Comments
Malli_S 10-Oct-11 7:24am    
What actually you wanna to ask? Because, you cant have both the functions declared at the same time in the same namespace/class.

It is not possible to determine the answer to your question as there is no way to figure out from the function call alone which version should be used. Therefore the C++ language doesn't allow to define overloads that differ only in return type!

Apart from that, my advice is to not make your functions do unexpected things: If you define a function add(int, int) then everyone will expect it to return an int, no matter what return type you list. So, do everyone (and yourself) a favor and make the return type int! This is specifically important because when you look at the code where a function is called you can not see the function's return type!
 
Share this answer
 
I did not get your question fully.But, You can not overload a function by differing only the return type.So you can not declare both above functions at a time. either you can declare\define int add(int,int) or float add(int,int).According to the declaration\definition your return type will depends.
 
Share this answer
 
It will depend on the left hand side variable type.

If int it will return int, if float it will return float.
 
Share this answer
 
you can use template to write a function like this

template <typename T>
T TestAdd(T t1,T t2)
{
return t1 + t2;
}

float fValue1 = 10.0f, fValue2 = 20.0f;
float fValue3 = TestAdd(fValue1,fValue2);
// fValue3 = 30.0f

int nValue1 = 10, nValue2 = 2;
int nValue3 = TestAdd(nValue1,nValue2);
// nValue3 = 12

CString strValue1 = _T("123456");
CString strValue2 = TestAdd(strValue1,strValue1);
// strValue2 = _T("123456123456");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900