简体   繁体   English

C ++和CONST:如何在FLAG的typedef位置使用UINT32并使用CONST FLAG标志; 内部结构?ong

[英]C++ and CONST: How to use UINT32 in the typedef place of FLAG and use CONST FLAG flag; inside the structure?ong

I am new to C++ programming. 我是C ++编程的新手。 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. 当我将FLAG数据类型更改为UNIT32时,FLAG&会返回其他值。 If i use FLAG instead of FLAG&, then it is behaving properly. 如果我使用FLAG而不是FLAG&,则表示行为正常。

Please help me to understand the above. 请帮助我理解以上内容。

Thanks in advance. 提前致谢。

CONST is not part of C++, I would guess it is a macro of some kind. CONST不是C ++的一部分,我想它是某种宏。

FLAG & flag;

would create a reference to a flag object. 将创建对标记对象的引用。 Exactly what the code means/does is impossible to say without further information. 没有进一步的信息,代码的确切含​​义是不可能说的。

flag is a const member, so that it can only be initialized, not assigned to, unless there's a cast involved. flagconst成员,因此除非涉及到强制类型转换,否则只能将其初始化而不分配给它。 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. flag是一个引用( & ),因此它不保留自己的值,而是保留其他位置的值。 A reference is simply a different name for another variable (or possibly a value, if it's a const ref). 引用只是另一个变量的另一个名称(如果是const ref,则可能是一个值)。 If you initialize it with a variable called i , for example, then it's another name for i . 例如,如果您使用名为i的变量对其进行初始化,则它是i的另一个名称。 If the value of i changes, the value of flag changes. 如果i的值更改,则flag的值更改。 The const means that nothing in x can modify it directly, not that the value can't possibly be modified. const表示x中没有任何东西可以直接修改它,而不是值不可能被修改。 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 and FLAG & , which indicates that you are initializing it with a variable, and the variable is then getting changed. 您确实提到过使用FLAGFLAG &获得不同的结果,这表明您正在使用变量对其进行初始化,然后该变量被更改。 Given more context, we could provide more detail. 给定更多上下文,我们可以提供更多细节。

Now, if you've provided the actual code, there is no difference between UINT16 and UINT32 , since you've defined them both as unsigned short . 现在,如果您提供了实际的代码,则UINT16UINT32之间没有区别,因为您已将它们都定义为unsigned 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. 如果存在,则意味着您不仅提供的代码不足以了解正在发生的情况,而且还提供的代码与实际获取结果的方式不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM