Hi,
I have a function (prototype):
Code:void MouseButtonDown(int x, int y, int iBut:2);
Is this valid?Code:void MouseButtonDown(int x, int y, int iBut) { switch (iBut) { case (00): { etc...
Hi,
I have a function (prototype):
Code:void MouseButtonDown(int x, int y, int iBut:2);
Is this valid?Code:void MouseButtonDown(int x, int y, int iBut) { switch (iBut) { case (00): { etc...
What are you trying to achieve? I believe your function prototype is syntactically invalid.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I'm with laserlight: Unless you have compiler extensions that allow you to pass values like that, the code should not compile.
If you are using C++, you could use templates to implement numbers that are specific number of bits, but there's no space benefit in doing so.
Bitfields are only allowed in structs/classes (and perhaps unions).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
He probably wants to have a 2bit variable. I don't think that is possible.
As far as I know you can have bit fields only in a struct. Like:
Now, the point is that you will declare the function normally to accept an int. Then you will make a struct that has one variable with :2. So it will be a struct which has a twoBit variable. Then you will pass the smallInt.twoBit int to the function and you will have what you want. You won't do though case (00): You will do normally case (0), case(1), case(2), case(3).Code:typdef struct { int twoBit:2; } smallInt;
If you assign a value greater than 3 on a 2 bit int then only the 2 last digits will be assign, thus, since 4 = 100 you will have 00, thus your int will be 0.
Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.
Indeed, so if the reason is to save space, then perhaps it is misguided. If the reason is to restrict the range to 4 values, then perhaps the real reason for the range is that there are a specific number of say, events, that are acceptable, so perhaps an enum or a full blown class tht emulates an enum would be better.Originally Posted by C_ntua
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
And you can combine the enums with the | operator. So you can pass two or three options with one parameter. Like MouseMoveDown(...., option1 | option2 | option 3) and then use a mask (using the &) to get individual bits/options