我认为我从cplusplus.com和MSDN获得的信息与C ++中的“函数定义”完全相同。
MSDN seems to include the parameters:
<div class="rate-ex3-cnt" id="<?php echo $productArray[$key]["ID"];?>
<div id="1" value="1" class="rate-btn-1 rate-btn"></div>
<div id="2" value="2" class="rate-btn-2 rate-btn"></div>
<div id="3" value="3" class="rate-btn-3 rate-btn"></div>
<div id="4" value="4" class="rate-btn-4 rate-btn"></div>
<div id="5" value="5" class="rate-btn-5 rate-btn"></div>
</div>
<script>
// rating script
$(function(){
$('.rate-btn').hover(function(){
$('.rate-btn').removeClass('rate-btn-hover');
var therate = $(this).attr('id');
for (var i = therate; i >= 0; i--) {
$('.rate-btn-'+i).addClass('rate-btn-hover');
};
});
$('.rate-btn').click(function(){
var therate = $(this).attr('id');
var dataRate = 'act=rate&post_id=<?php echo $post_id; ?>&rate='+therate; //
$('.rate-btn').removeClass('rate-btn-active');
for (var i = therate; i >= 0; i--) {
$('.rate-btn-'+i).addClass('rate-btn-active');
};
$.ajax({
type : "POST",
url : "http://localhost/rating/ajax.php",
data: dataRate,
success:function(){}
});
});
});
</script>
该函数可以从中的任意数量的位置调用或调用 程序。传递给函数的值是参数, 其类型必须与。中的参数类型兼容 功能定义。
而cplusplus does not,暗示函数的正文(或者它是返回表达式/值?)是它的定义:
重载的函数可能具有相同的定义。例如:
int sum(int a, int b)
{
return a + b;
}
谷歌搜索“函数定义c ++”获得了很多函数的定义,我不关心。
那么,函数的哪些组件构成了它的定义?
答案 0 :(得分:5)
让我们直截了当地说明一些术语:
一些例子:
// declaration/prototype
void // return type
f // function name
(int) // function parameter list
; // semicolon
// definition
int g(double) // prototype part of the definition
{ return 42; } // the body, which really "defines" the function
// signature - in between the template's angle brackets < >
std::function<
int(double) // this bit is what one would call the signature
> h;
签名确定函数(指针)类型,以及在链接器开始将所有内容链接在一起时唯一标识函数的签名+名称。
为什么cplusplus.com说两个函数可以有相同的定义?嗯,这是错的,至少在这个例子中是这样的:
int sum(int a, int b) { return a+b; }
double sum(double a, double b) { return a+b; }
尽管函数体看起来相同,但它们表达了不同的基本行为:在整数情况下,+
表示整数加法,在后一种情况下,它是浮点加法。这是两个不同的(内置)运算符。总而言之,这只是一个令人困惑的例子。