I have a struct:
struct s
{
UINT_PTR B_ID;
};
s d;
d.B_ID=0x1;
That works fine, but I want d.B_ID to be constant. I tried to use (const) but it didn’t work. So after I put a value to d.B_ID, then I want make it a constant.
Any ideas?
EDIT
ok i don’t want the whole struct a constant.
when i set timer and use the b.B_ID as an idea for the timer.
in the
switch(wparam)
{
case b.B_ID // error: B_ID must be constant
….
break;
}
so that is why i need it to be a constant
Variable modifiers are fixed at compile time for each variable. You may have to explain the context of what you are trying to do, but perhaps this will suit your needs?
Since you are using C++ I would recommend:
Ramiz Toma: well i need way to do it using the s.B_ID=somethingIn C/C++ type modifiers (like const) are declared at run time for a given type and cannot be changed at run time. This means that if a variable is declared
constit can never be assigned to using the assignment operator. It will only be assigned a value when it is constructed.This is not a problem however because you can always get around this by proper design.
If you say you need to use assignment, I assume that this is because you create the struct before you know what the value of the variable will be. If this is the case then you simply need to move the struct declaration till after you know the value.
For example
This will not work, because if you want b.D_ID to be ‘un assignable’ it will always be so. You will need to refactor your code similarly to: