简体   繁体   中英

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. 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.

CONST is not part of C++, I would guess it is a macro of some kind.

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. 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 a const ref). If you initialize it with a variable called i , for example, then it's another name for i . If the value of i changes, the value of flag changes. The const means that nothing in x 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 and FLAG & , 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 and UINT32 , since you've defined them both as 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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