<分区>
当我观察到这一点时,我正在编写一些问题。由于函数应该是一组具有名称的语句,可以从程序的某个位置调用它们。考虑一个给出整数绝对值的简单程序:
#include <iostream>
#include <vector>
using namespace std;
int getAbsolute(int x) {
return x > 0 ? x : -1*x;
}
int main() {
vector<int> arr;
for(int i = -5; i < 5; i++)
arr.push_back(i);
for(int i = 0; i < arr.size(); i++) {
cout << "abs(" << arr[i] << ") : "
<< getAbsolute << endl;
}
}
当我运行这个程序时:
rohan@~/Dropbox/cprog/demos : $ g++ testFunction.cpp
rohan@~/Dropbox/cprog/demos : $ ./a.out
abs(-5) : 1
abs(-4) : 1
abs(-3) : 1
abs(-2) : 1
abs(-1) : 1
abs(0) : 1
abs(1) : 1
abs(2) : 1
abs(3) : 1
abs(4) : 1
rohan@~/Dropbox/cprog/demos : $
我的问题是,为什么这个程序没有给我应该用参数调用的错误,我的 g++(-v 4.8.5) 有问题吗?为什么这个程序在每次调用时输出 1?或者我在这里遗漏了什么?我真的很困惑。