在C++中使用命名空间
Use of namespace in C++?
#include "d3dApp.h"
#include <WindowsX.h>
#include <sstream>
namespace
{
// This is just used to forward Windows messages from a global window
// procedure to our member function window procedure because we cannot
// assign a member function to WNDCLASS::lpfnWndProc.
D3DApp* gd3dApp = 0;
}
LRESULT CALLBACK
MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Forward hwnd on because we can get messages (e.g., WM_CREATE)
// before CreateWindow returns, and thus before mhMainWnd is valid.
return gd3dApp->MsgProc(hwnd, msg, wParam, lParam);
}
我对C++命名空间的这种使用感到好奇。我开始阅读有关命名空间的文档,我看到很多示例调用命名空间的名称,例如"命名空间优先",但没有一个在命名空间声明之后没有任何内容。
这是一个
匿名或未命名的命名空间。 命名空间中的项(在此示例中只是gd3dApp
(在翻译单元中可见,但不能在外部引用,因为没有名称来限定它们。
注意:这不会阻止外部链接。 看看这里: http://msdn.microsoft.com/en-us/library/yct4x9k5(v=vs.80(.aspx.
尽管未命名命名空间中的实体可能具有外部链接,但它们实际上由其翻译单元独有的名称限定,因此永远无法从任何其他翻译单元中看到。
这种技术略优于static
,因为它也适用于typedef
秒(不能声明为static
(。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 是否可以将函数导入命名空间,但不能导出它?
- C++ C++类中的命名空间降级
- 如何使用 soong 命名空间来有条件地编译模块
- 使用 Clang++ 有没有办法将文件作为命名空间等包含?
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 如何通过命名空间调用非静态方法
- 在命名空间名称之前加上 :: 是什么意思?