C++中的std,一点理解

#include <iostream>
void func(){
    //必须重新声明
    using namespace std;
    cout<<"Hello World"<<endl;
}
int main(){
    //声明命名空间std
    using namespace std;
    cout<<"C++"<<endl;
    func();
    return 0;
}
#include <iostream>
//声明命名空间std
using namespace std;
void func(){
    cout<<"Hello World"<<endl;
}
int main(){
    cout<<"C++"<<endl;
    func();
    return 0;
}

C++很多教程中都是这样做的,将 std 直接声明在所有函数外部,这样虽然使用方便,但在中大型项目开发中是不被推荐的。
这样做增加了命名冲突的风险,推荐在函数内部声明 std。

发布了30 篇原创文章 · 获赞 5 · 访问量 2204

猜你喜欢

转载自blog.csdn.net/weixin_44408476/article/details/105195989