我将整数值传递给给定函数它将返回int或float [英] i pass value in integer to given function what will it return int or float

查看:139
本文介绍了我将整数值传递给给定函数它将返回int或float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果将值作为整数传递给给定的函数,则为
int add(int,int)
浮动add(int,int)

那么它给出的结果将作为float/integer

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

推荐答案

返回的结果,由于无法从中确定,因此无法确定问题的答案仅函数调用应使用哪个版本.因此,C ++语言不允许定义仅在返回类型上不同的重载!

除此之外,我的建议是不要让函数做意外的事情:如果定义函数add(int, int),那么无论您列出哪种返回类型,每个人都希望它返回int.因此,请每个人(和您自己)帮个忙,并使返回类型为int!这一点特别重要,因为当您查看调用函数的代码时,看不到函数的返回类型!
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!


我没有完全回答您的问题.但是,您不能通过仅更改返回类型来重载函数.因此,您不能一次声明上述两个函数.您可以声明\ define int add(int,int)或float add(int,int).根据声明\ definition,您的返回类型将取决于.
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.


这将取决于左侧变量类型.

如果为int,则返回int,如果为float,则返回float.
It will depend on the left hand side variable type.

If int it will return int, if float it will return float.


这篇关于我将整数值传递给给定函数它将返回int或float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆