“使用”的范围是什么,声明在C ++? [英] What's the scope of the "using" declaration in C++?

查看:88
本文介绍了“使用”的范围是什么,声明在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++中的'using'声明将std :: string和std :: vector添加到本地命名空间(以保存键入不必要的'std ::')。

I'm using the 'using' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary 'std::'s).

using std::string;
using std::vector;

class Foo { /*...*/ };

此声明的范围是什么?如果我在头中这样做,它会注入这些使用声明到每个包含头的cpp文件中?

What is the scope on this declaration? If I do this in a header, will it inject these 'using' declarations into every cpp file that includes the header?

推荐答案

当你在C ++中包含一个头文件时,它会将头文件的全部内容放在你在源文件中包含的位置。因此,包含具有使用声明的文件与将使用声明放置在每个包含该头文件的文件。

When you #include a header file in C++, it places the whole contents of the header file into the spot that you included it in the source file. So including a file that has a using declaration has the exact same effect of placing the using declaration at the top of each file that includes that header file.

这篇关于“使用”的范围是什么,声明在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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