花括号内部结构的声明
Declaration of the struct inside of the curly bracers
出于某种原因,结构 window 在花括号内部声明和使用,为什么作者要这样做?
// separated header with defined struct
typedef struct SIZE
{
UINT Width;
UINT Height;
} SIZE;
.....
// .cpp file that uses header with SIZE but doesn't have a straightforward relation to header
void foo(UINT W, UINT H)
{
// magic
{
SIZE window = {};
window.Width = W;
window.Height= H;
// magic with window
}
// magic
}
在
void foo(UINT W, UINT H)
{
// magic
{
SIZE window = {};
window.Width = W;
window.Height= H;
}
// magic
}
额外的花括号建立了 window
的 compound statement, also known as a block. In this example, this block doesn't do anything except establish the scope。 window
只存在于区块内;一旦到达右大括号,window
将被销毁。
旁注:
window
的初始化可以大大简化:
void foo(UINT W, UINT H)
{
// magic
{
SIZE window = {W,H};
}
// magic
}
另一个注意事项:除了常量之外,避免使用 ALLCAPS 标识符。它可能导致使用先前定义的常量 just like this poor soul found out the hard way 进行意外的宏替换。此外,应避免使用单字母变量。单个字母往往不足以描述变量所代表的内容,而且它们很容易不小心混淆,同时也很难在混淆时发现。
出于某种原因,结构 window 在花括号内部声明和使用,为什么作者要这样做?
// separated header with defined struct
typedef struct SIZE
{
UINT Width;
UINT Height;
} SIZE;
.....
// .cpp file that uses header with SIZE but doesn't have a straightforward relation to header
void foo(UINT W, UINT H)
{
// magic
{
SIZE window = {};
window.Width = W;
window.Height= H;
// magic with window
}
// magic
}
在
void foo(UINT W, UINT H)
{
// magic
{
SIZE window = {};
window.Width = W;
window.Height= H;
}
// magic
}
额外的花括号建立了 window
的 compound statement, also known as a block. In this example, this block doesn't do anything except establish the scope。 window
只存在于区块内;一旦到达右大括号,window
将被销毁。
旁注:
window
的初始化可以大大简化:
void foo(UINT W, UINT H)
{
// magic
{
SIZE window = {W,H};
}
// magic
}
另一个注意事项:除了常量之外,避免使用 ALLCAPS 标识符。它可能导致使用先前定义的常量 just like this poor soul found out the hard way 进行意外的宏替换。此外,应避免使用单字母变量。单个字母往往不足以描述变量所代表的内容,而且它们很容易不小心混淆,同时也很难在混淆时发现。