C++和 CONST:如何在 FLAG 的 typedef 位置使用 UINT32 并使用 CONST FLAG 标志;结构内部?ong
我是 C++ 编程新手。我正在分析代码,发现以下内容:
typedef unsigned short UINT16;
typedef unsigned long UINT32;
typedef UNIT16 FLAG;
//within a structure,
struct x
{
const FLAG& flag; //what this means??
};
当我将 FLAG 数据类型更改为 UNIT32 时,然后 FLAG&我猜正在返回一些其他值。如果我使用 FLAG 而不是 FLAG&,那么它的行为正常。
请帮助我理解以上内容。
提前致谢。
I am new to C++ programming. I am analyzing a code and I found the following:
typedef unsigned short UINT16;
typedef unsigned long UINT32;
typedef UNIT16 FLAG;
//within a structure,
struct x
{
const FLAG& flag; //what this means??
};
When I change the FLAG datatype to UNIT32, then FLAG& is returning some other value i guess. If i use FLAG instead of FLAG&, then it is behaving properly.
Please help me to understand the above.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CONST 不是 C++ 的一部分,我猜它是某种宏。
将创建对标志对象的引用。如果没有进一步的信息,就不可能确切地说出代码的含义/作用。
CONST is not part of C++, I would guess it is a macro of some kind.
would create a reference to a flag object. Exactly what the code means/does is impossible to say without further information.
flag
是一个const
成员,因此只能对其进行初始化,而不能对其进行赋值,除非涉及强制转换。您得到的结果取决于您初始化它的内容,但您不会显示。flag
是一个引用(&
),因此它不保留自己的值,而是保存来自其他地方的值。引用只是另一个变量的不同名称(或者可能是一个值,如果它是 const 引用)。例如,如果您使用名为i
的变量来初始化它,那么它就是i
的另一个名称。如果i
的值改变,flag
的值也会改变。 const 意味着 x 中的任何内容都不能直接修改它,而不是说该值不可能被修改。同样,由于没有有关您正在执行的初始化的信息,因此无法解释发生了什么。您确实提到过
FLAG
和FLAG &
得到了不同的结果,这表明您正在使用变量对其进行初始化,然后该变量正在发生更改。考虑到更多背景,我们可以提供更多细节。现在,如果您提供了实际的代码,则
UINT16
和UINT32
之间没有区别,因为您已将它们都定义为unsigned Short
>。行为上不应有任何差异。如果有,则意味着您提供的代码不仅不足以了解正在发生的情况,而且提供的代码与实际获取结果的代码不同。flag
is aconst
member, so that it can only be initialized, not assigned to, unless there's a cast involved. What results you get depend on what you initialize it with, which you do not show.flag
is a reference (the&
), so it doesn't keep its own value, but rather holds a value from somewhere else. A reference is simply a different name for another variable (or possibly a value, if it's aconst
ref). If you initialize it with a variable calledi
, for example, then it's another name fori
. If the value ofi
changes, the value offlag
changes. Theconst
means that nothing inx
can modify it directly, not that the value can't possibly be modified. Again, given no information about the initialization you're doing, it's impossible to explain what's going on.You did mention that you got different results with
FLAG
andFLAG &
, which indicates that you are initializing it with a variable, and the variable is then getting changed. Given more context, we could provide more detail.Now, if you've provided the actual code, there is no difference between
UINT16
andUINT32
, since you've defined them both asunsigned short
. There should be no difference in behavior. If there is, it means that you're providing not only insufficient code to know what's going on, but different code than you're actually getting your results from.