为什么微软使用“g_” DirectX10 管道变量的命名约定?
Microsoft DirectX SDK 中的大多数示例代码都包含使用 g_
前缀表示 Windows API 变量的变量,以及 DirectX 管道变量(例如交换链)。
以下是一些示例:
D3D10_DRIVER_TYPE g_driverType;
ID3D10Device* g_pd3dDevice;
IDXGISwapChain* g_pSwapChain;
ID3D10RenderTargetView* g_pRenderTargetView;
ID3D10Effect* g_pEffect;
ID3D10EffectTechnique* g_pTechnique;
ID3D10InputLayout* g_pVertexLayout;
ID3D10Buffer* g_pVertexBuffer;
ID3D10Buffer* g_pIndexBuffer;
ID3D10EffectMatrixVariable* g_pWorldVariable;
ID3D10EffectMatrixVariable* g_pViewVariable;
ID3D10EffectMatrixVariable* g_pProjectionVariable;
D3DXMATRIX g_World;
D3DXMATRIX g_View;
D3DXMATRIX g_Projection;
这背后的原因是什么?我不明白 g_
的含义,以及为什么您不使用“SwapChain”等更方便的名称。谁能解释一下吗?
Most of the sample code from Microsoft's DirectX SDK includes variables which use the g_
prefix for Windows API variables, as well as DirectX pipeline variables, such as the swapchain.
Here are some samples:
D3D10_DRIVER_TYPE g_driverType;
ID3D10Device* g_pd3dDevice;
IDXGISwapChain* g_pSwapChain;
ID3D10RenderTargetView* g_pRenderTargetView;
ID3D10Effect* g_pEffect;
ID3D10EffectTechnique* g_pTechnique;
ID3D10InputLayout* g_pVertexLayout;
ID3D10Buffer* g_pVertexBuffer;
ID3D10Buffer* g_pIndexBuffer;
ID3D10EffectMatrixVariable* g_pWorldVariable;
ID3D10EffectMatrixVariable* g_pViewVariable;
ID3D10EffectMatrixVariable* g_pProjectionVariable;
D3DXMATRIX g_World;
D3DXMATRIX g_View;
D3DXMATRIX g_Projection;
What is the reasoning behind this? I'm not getting what the g_
signifies, and why you wouldn't use more convenient names like "SwapChain." Can anyone explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
g_
通常代表全局变量。由于历史原因,Microsoft 文档和示例中提供的(非 .NET)代码可能使用匈牙利表示法的某些变体。系统 匈牙利表示法在 C++ 中使用得很少(如果有的话),因为编译器已经知道变量的类型。有一种应用程序匈牙利表示法,Joel Spolsky 已为此撰写了一篇文章。
现在,全局变量在生产代码中并不是一个好主意。全局变量可以随处访问,这意味着它们可以在代码中随时随地进行修改。这很容易成为维护和调试的噩梦。
您在示例代码中看到它们的原因是因为示例是最小但可编译的代码片段,用于演示如何使用 API。请注意,出于同样的原因,示例代码也省略了错误检查等内容。示例代码不一定展示良好的编码技术或实践,尽管它当然是可能的。
简而言之,“示例样式”代码在任何重要的应用程序中都会很快变得混乱。在生产代码中,您应该建立一个框架并进行适当的代码设计。这包括不使用全局变量。
The
g_
typically stands for global variable. The (non-.NET) code presented in Microsoft documentation and samples may use some variant of Hungarian notation for historical reasons.Systems Hungarian notation isn't used very much, if at all, in C++ since the compiler already knows the types of your variables. There is such a thing as Applications Hungarian notation, for which Joel Spolsky has written an article about.
Now, global variables are not a good idea in production code. Global variables are accessible everywhere, meaning that they can be modified whenever and wherever in your code. That easily becomes a maintenance and debugging nightmare.
The reason why you see them in sample code is because samples are meant to be minimal but compilable code snippets that demonstrates how to use the API. Notice that sample code also omits things like error-checking for the same reason. Sample code do not necessarily demonstrate good coding techniques or practices, although it's certainly possible.
In a nutshell, "sample-style" code becomes messy real quick in any non-trivial application. In production code, you should be setting up a framework and do a proper code design. That includes not using global variables.