我正在替换我的#defines,例如#define NUM_SLIDER_POSITIONS 5用于常量变量.我应该保留旧的命名,如:
const unsigned int NUM_SLIDER_POSITIONS = 5;
Run Code Online (Sandbox Code Playgroud)
或者我应该使用更像:
const unsigned int kNumSliderPositions = 5;
Run Code Online (Sandbox Code Playgroud)
.
编辑:帖子已被搁置,但无论如何我想总结你的答案:
其他选项是使用下划线作为使用小写字母的分隔符:
const unsigned int num_slider_positions = 5;
Run Code Online (Sandbox Code Playgroud)
常量标识符.
关于使用前缀作为识别常量的方法,最常见的选项是不使用它,因为它可能不会添加相关信息:
const unsigned int num_slider_positions = 5;
Run Code Online (Sandbox Code Playgroud)
在名称前使用"k":
const unsigned int k_num_slider_positions = 5;
Run Code Online (Sandbox Code Playgroud)
或者在类或命名空间中声明变量,以避免污染全局范围并提供更加不言自明的名称:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Run Code Online (Sandbox Code Playgroud)
客户代码:
int slider_positions = defaults::num_slider_positions;
Run Code Online (Sandbox Code Playgroud) 以下代码;
struct s1 {
void *a;
char b[2];
int c;
};
struct s2 {
void *a;
char b[2];
int c;
}__attribute__((packed));
Run Code Online (Sandbox Code Playgroud)
如果s1大小为12个字节并且s2大小为10个字节,这是由于数据是以4个字节的块读取还是}__attribute__((packed));将大小减小void*a;到只有2个字节?
有点困惑的是什么}__attribute__((packed));.
非常感谢