C++代码运行时会丢失标题,为什么?
问题描述:
我刚刚意识到我应该包含abs()
功能abs()
所需的#include<cstdlib>
。C++代码运行时会丢失标题,为什么?
#include<iostream>
using namespace std;
int main()
{
int result;
result = abs(-10);
cout << result << "\n";
return 0;
}
为什么这个代码仍然可以工作,即使我忘了重要的头部(#include<cstdlib>
)?
答
这是因为iostream
间接包含abs()
的定义。这是标准允许的,但不应该依赖,因为它依赖于实现(即,您的代码可能无法在其他编译器上编译)。
+2
换句话说,只包括#include
什么是你的平台,编译器? – 2009-12-23 09:51:55
Win7Prof Visual C++ 2008 Express Edition – Newb 2009-12-23 09:57:07
我想这也取决于编译器.. btw:不错的问题+1 – 2009-12-23 10:39:59