C ++代码在缺少标头的情况下运行,为什么? [英] C++ code runs with missing header, why?

查看:82
本文介绍了C ++代码在缺少标头的情况下运行,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到我应该为 abs()函数包含 abs()所需的#include< cstdlib>

I just realized that I am supposed to include the #include<cstdlib> required by abs() for the abs() function.

 #include<iostream>
    using namespace std;
    int main()
    {
        int result;
        result = abs(-10);
        cout << result << "\n";
        return 0;

    }

即使我忘记了重要的标头(#include< cstdlib> ),为什么此代码仍然可以工作?

Why does this code still work, even though I forgot the important header (#include<cstdlib>)?

推荐答案

这是因为 iostream 间接包含了 abs()的定义.标准允许,但不应依赖它,因为它取决于实现(即,您的代码可能无法在其他某些编译器上编译).

That's because iostream indirectly includes definition for abs(). It is allowed by the Standard, but should not be relied upon, because it's implementation-dependant (i.e. your code may not compile on some other compilers).

这篇关于C ++代码在缺少标头的情况下运行,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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