views:

49

answers:

2

Hello All,

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.

A: 

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.

anon
const is not a macro. It is constant keyword.
MalarN
@MalarN C++ is case-sensitive.
anon
The keyword is in small letters... "const"
MalarN
A: 

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.

David Thornley