我可以在自定义命名空间中包含iostream头文件吗? [英] Can I include iostream header file into custom namespace?

查看:376
本文介绍了我可以在自定义命名空间中包含iostream头文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 命名空间A 
{
#include< iostream>
};

int main(){
A :: std :: cout< \\\
Sample;
return 0;
}


解决方案

/ p>

长答案:嗯,不是真的。你可以假装它,虽然。您可以在命名空间中声明它,并使用using语句,如下所示:

  #include< iostream> 

命名空间A
{
使用std :: cout;
};

int main(){
A :: cout< \\\
Sample;
system(PAUSE);
return 0;
}

您不能本地化库,因为即使它在A中有访问权限将不能在标准命名空间中访问。



此外,另一个问题是,命名空间中的限定名称将是A :: std :: cout,但是库不会包含使用外部命名空间。感谢Jonathon Leffler。



如果问题是你不想让其他人知道你的代码可以做什么,你可以有自己的cpp文件包含iostream,有定义的命名空间。然后你只需将它包含在main(或其他)中,让程序员知道他能做什么和不能做什么。


namespace A
{
   #include <iostream>
};

int main(){
 A::std::cout << "\nSample";
 return 0;
}

解决方案

Short answer: No.

Long answer: Well, not really. You can fake it, though. You can declare it outside and use using statements inside the namespace, like this:

#include <iostream>

namespace A
{
   using std::cout;
};

int main(){
 A::cout << "\nSample";
 system("PAUSE");
 return 0;
}

You cannot localize a library, because even if it had access in A, it would not have access in the standard namespace.

Also, "The other problem is that the qualified names inside the namespace would be A::std::cout, but the library would not contain names qualified with the outer namespace." thanks Jonathon Leffler.

If the problem is that you don't want to let other people know what all your code can do, you could have your own cpp file to include iostream in, and have the namespace defined there. Then you just include that in main (or whatever) and let the programmer know what he can and cannot do.

这篇关于我可以在自定义命名空间中包含iostream头文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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